#include <tft_lcd.h>
#include <pronano.h>
#include <screen_page.h>

/*20130419 added by michael
Assay_index, 0:BCA, 1:Bradford, 2:Pierce*/
int Assay_index = -1, menu_index = -1, measure_index = -1;
extern volatile int8_t User_key;
struct Measure_sheet sheet_info[MAX_Sheets];
int Select_sheet = -1;

/*20130417 added by michael
if start_up==1 then */
void Show_logo(int start_up)
{
  int i = 0;

/*display the corporation logo*/
#if defined(LOCCUS_ODM) || defined(TIANGEN_ODM)
  for (i = sizeof(gImage_pronano_logo) / 12 - 2; (i <= sizeof(gImage_pronano_logo) / 12 - 1); i++) {
     TFT_Send_Command(gImage_pronano_logo[i], sizeof(gImage_pronano_logo[i]),0);
  }
  Delay100ms(20);
#else
  for (i = sizeof(gImage_pronano_logo) / 12 - 1; 0 <= i; i--) {
     while (TFT_Busy)
       Delay100ms(1);
     TFT_Send_Command(gImage_pronano_logo[i], sizeof(gImage_pronano_logo[i]),0);
     Delay100ms(8);
#ifdef TFT_DEBUG
//  getchar();
#endif  //TFT_DEBUG
  }
#endif  //defined(LOCCUS_ODM) || defined(TIANGEN_ODM)

/*Main menu item "About MaestroNano*/
  if (!start_up) {
    for (i = 0; i < sizeof(gPreface_page1) / 12; i++) {
       while (TFT_Busy)
         Delay10ms(1);
       TFT_Send_Command(gPreface_page1[i], sizeof(gPreface_page1[i]),0);
    }

//wait enter key
#ifdef Keyboard_Emu
	while (getchar() != Enter_key);
#else
	while(Get_key_queue(0) != Enter_key);
#endif  //Keyboard_Emu

#if !defined(LOCCUS_ODM)
    for (i = 0; i < sizeof(gPreface_page2) / 12; i++) {
       while (TFT_Busy)
         Delay10ms(1);
       TFT_Send_Command(gPreface_page2[i], sizeof(gPreface_page1[i]),0);
    }
#ifdef Keyboard_Emu
	while (getchar() != Enter_key);
#else
	while(Get_key_queue(0) != Enter_key);
#endif  //Keyboard_Emu, wait enter key
#endif
#if !defined(DEF_ODM) || (defined(DEF_ODM) && defined(TIANGEN_ODM))
    for (i = 0; i < sizeof(gPreface_page3) / 12; i++) {
       TFT_Send_Command(gPreface_page3[i], sizeof(gPreface_page1[i]),0);
    }
#ifdef Keyboard_Emu
	while (getchar() != Enter_key);
#else
	while(Get_key_queue(0) != Enter_key);
#endif
#endif
  }
}


/*20130429 added by michael
Protein Assay selection*/
void Display_Assay_Page(void) {
	int8_t key;
	int New_Assay_index = 0;

	Assay_index = 0;

ReDraw_Assay_Page:
	TFT_Send_Command(gMain_menu_page[0], sizeof(gMain_menu_page[0]),0);
	TFT_Send_Command(gProtein_Assay_page[0], sizeof(gProtein_Assay_page[0]),0);
	TFT_Send_Command(gProtein_Assay_page[1], sizeof(gProtein_Assay_page[1]),0);
	TFT_Send_Command(gProtein_Assay_page[8], sizeof(gProtein_Assay_page[8]),0);

	TFT_Send_Command(gProtein_Assay_page[2], sizeof(gProtein_Assay_page[2]),0);
	TFT_Send_Command(gProtein_Assay_page[4], sizeof(gProtein_Assay_page[4]),0);
	TFT_Send_Command(gProtein_Assay_page[6], sizeof(gProtein_Assay_page[6]),0);
	TFT_Send_Command(gProtein_Assay_page[2+2*Assay_index+1], sizeof(gProtein_Assay_page[2]),0);
  

#ifdef Keyboard_Emu
	while ((key = getchar()) != Enter_key) {
#else
	while (1) {
#ifdef USE_KEY_QUEUE
		key = Get_key_queue(1);
#else
		key = User_key;
		User_key = Null_key;
#endif  //USE_KEY_QUEUE

#endif  //Keyboard_Emu

		if (key==Enter_key)
			break;
		else
			if (key==Null_key)
				continue;
			else
				if (key==Refresh_key) {
					goto ReDraw_Assay_Page;
					printf("refresh screen\n");
				}

		switch(key) {
		  case Up_key:
			  if (Assay_index > 0)
				  New_Assay_index = Assay_index - 1;
			  break;
		  case Down_key:
			  if (Assay_index < 2)
				  New_Assay_index = Assay_index + 1;
			  break;
		  case Power_key:
			  if (!is_Power_Down()) {
				  //mode_index = Region_index - 11;
				  goto ReDraw_Assay_Page;
			  }
			  else {
				  outpw((uint32_t)&GPIOE->DOUT, inpw((uint32_t) & GPIOE->PIN) & ~(1 << 5));
				  while(1);
			  }
			  break;
		}

//highlight the active assay
		if (New_Assay_index != Assay_index) {
			TFT_Send_Command(gProtein_Assay_page[2], sizeof(gProtein_Assay_page[2]),0);
			TFT_Send_Command(gProtein_Assay_page[4], sizeof(gProtein_Assay_page[4]),0);
			TFT_Send_Command(gProtein_Assay_page[6], sizeof(gProtein_Assay_page[6]),0);
			Assay_index = New_Assay_index;
			TFT_Send_Command(gProtein_Assay_page[2+2*Assay_index+1], sizeof(gProtein_Assay_page[2]),0);
		}
	}
}

/*20130506 added by michael*/
void Display_measure_output1(struct Measure_tran *pMea_tran, int type, int sample_index) //type: 0,blank; type: 1,BCA; type: 2,Bradford; type: 3,Pierce
{
	char buf[30], *pch = NULL;
#ifdef LOCCUS_ODM
	int offset_x = -8;
#else
	int offset_x = 0;
#endif

	printf("Display_measure_output1, type:%d, sample index: %d\n", type, sample_index);
	switch (type) {
	  case 0:
		  TFT_Send_Command(g_Display_blank_output[23], sizeof(g_Display_blank_output[23]),0);  //show blank output result
#ifdef LOCCUS_ODM
		  sprintf(buf, "%02d", pMea_tran->date[0].PackDate.Day);
		  Show_LCD_Number(buf, g_Display_blank_output[0][5]+offset_x, g_Display_blank_output[0][7]);

		  sprintf(buf, "/%02d", pMea_tran->date[0].PackDate.Month);
		  Show_LCD_Number(buf, g_Display_blank_output[5][5]+2*8, g_Display_blank_output[5][7]);

		  sprintf(buf, "/%04d", pMea_tran->date[0].PackDate.Year);
		  Show_LCD_Number(buf, g_Display_blank_output[8][5]+2*8, g_Display_blank_output[8][7]);
#else
		  sprintf(buf, "%04d", pMea_tran->date[0].PackDate.Year);
		  Show_LCD_Number(buf, g_Display_blank_output[0][5]+offset_x, g_Display_blank_output[0][7]);

		  TFT_Send_Command(g_Display_blank_output[4], sizeof(g_Display_blank_output[4]),0);
		  sprintf(buf, "%02d", pMea_tran->date[0].PackDate.Month);
		  Show_LCD_Number(buf, g_Display_blank_output[5][5], g_Display_blank_output[5][7]);

		  TFT_Send_Command(g_Display_blank_output[7], sizeof(g_Display_blank_output[7]),0);
		  sprintf(buf, "%02d", pMea_tran->date[0].PackDate.Day);
		  Show_LCD_Number(buf, g_Display_blank_output[8][5], g_Display_blank_output[8][7]);
#endif

		  sprintf(buf, "%02d", pMea_tran->date[0].PackDate.Hour);
		  Show_LCD_Number(buf, g_Display_blank_output[10][5]+offset_x, g_Display_blank_output[10][7]);
//Delay100ms(1);
#ifdef LOCCUS_ODM
		  sprintf(buf, ":%02d", pMea_tran->date[0].PackDate.Minute);
#else
		  TFT_Send_Command(g_Display_blank_output[12], sizeof(g_Display_blank_output[12]),0);
		  sprintf(buf, "%02d", pMea_tran->date[0].PackDate.Minute);
#endif
		  Show_LCD_Number(buf, g_Display_blank_output[13][5], g_Display_blank_output[13][7]);
//Delay100ms(1);
#ifdef LOCCUS_ODM
		  sprintf(buf, ":%02d", pMea_tran->date[0].PackDate.Second);
#else
		  TFT_Send_Command(g_Display_blank_output[15], sizeof(g_Display_blank_output[15]),0);
		  sprintf(buf, "%02d", pMea_tran->date[0].PackDate.Second);
#endif
		  Show_LCD_Number(buf, g_Display_blank_output[16][5], g_Display_blank_output[16][7]);
//Delay100ms(1);
		  sprintf(buf, "%2.3f", (pMea_tran->blank_AXXX.Xenon_I-pMea_tran->blank_AXXX.Non_Xenon_I)/10000);
		  Show_LCD_Number(buf, (g_Display_blank_output[18][5] | g_Display_blank_output[18][4] << 8), g_Display_blank_output[18][7]);
		  break;
//562nm
	  case 1:
		  Delay100ms(1);
		  TFT_Send_Command(g_Display_blank_output[35], sizeof(g_Display_blank_output[35]),0);  //show sample output result(BCA)
		  sprintf(buf, "%3.3f", 1000*pMea_tran->AXXX[sample_index]);
		  Show_LCD_Number(buf, (g_Display_blank_output[38][5] | g_Display_blank_output[38][4] << 8), g_Display_blank_output[38][7]);  //OD562
		  break;

//595nm
	  case 2:
		  Delay100ms(1);
		  TFT_Send_Command(g_Display_blank_output[36], sizeof(g_Display_blank_output[36]),0);  //show sample output result(Bradford)
		  sprintf(buf, "%3.3f", 1000*pMea_tran->AXXX[sample_index]);
		  Show_LCD_Number(buf, (g_Display_blank_output[38][5] | g_Display_blank_output[38][4] << 8), g_Display_blank_output[38][7]);  //OD595
		  break;

//660nm
	  case 3:
		  Delay100ms(1);
		  TFT_Send_Command(g_Display_blank_output[37], sizeof(g_Display_blank_output[37]),0);  //show sample output result(Pierce)
		  sprintf(buf, "%4.3f", 10000*pMea_tran->AXXX[sample_index]);
		  Show_LCD_Number(buf, (g_Display_blank_output[38][5] | g_Display_blank_output[38][4] << 8), g_Display_blank_output[38][7]);  //OD600
		  break;

	}

	if (type > 0) {
		//sprintf(buf, "%2.3f", pMea_tran->AXXX[sample_index]);
		//Show_LCD_Number(buf, (g_Display_blank_output[38][5] | g_Display_blank_output[38][4] << 8), g_Display_blank_output[38][7]);  //OD562

		//sample_XX
		sprintf(buf, "%02d", pMea_tran->sample_No[sample_index]);
		Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Display_blank_output[39][5], g_Display_blank_output[39][7]);
		Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Display_blank_output[39][5]-8, g_Display_blank_output[39][7]);
#ifdef LOCCUS_ODM
#else
		//date: yyyy/mm/dd
		sprintf(buf, "%04d", pMea_tran->date[sample_index+1].PackDate.Year);
		Show_LCD_Number(buf, g_Display_blank_output[40][5], g_Display_blank_output[40][7]);

		Show_LCD_Number("/", g_Display_blank_output[40][5]-8*4, g_Display_blank_output[40][7]);
		sprintf(buf, "%02d", pMea_tran->date[sample_index+1].PackDate.Month);
		Show_LCD_Number(buf, g_Display_blank_output[40][5]-8*5, g_Display_blank_output[40][7]);

		Show_LCD_Number("/", g_Display_blank_output[40][5]-8*7, g_Display_blank_output[40][7]);
		sprintf(buf, "%02d", pMea_tran->date[sample_index+1].PackDate.Day);
		Show_LCD_Number(buf, g_Display_blank_output[40][5]-8*8, g_Display_blank_output[40][7]);
#endif

#ifdef LOCCUS_ODM
#else
		sprintf(buf, "%02d", pMea_tran->date[sample_index+1].PackDate.Hour);
		Show_LCD_Number(buf, g_Display_blank_output[41][5], g_Display_blank_output[41][7]);

		Show_LCD_Number("/", g_Display_blank_output[41][5]-8*2, g_Display_blank_output[41][7]);
		sprintf(buf, "%02d", pMea_tran->date[sample_index+1].PackDate.Minute);
		Show_LCD_Number(buf, g_Display_blank_output[41][5]-8*3, g_Display_blank_output[41][7]);

		Show_LCD_Number("/", g_Display_blank_output[41][5]-8*5, g_Display_blank_output[41][7]);
		sprintf(buf, "%02d", pMea_tran->date[0].PackDate.Second);
		Show_LCD_Number(buf, g_Display_blank_output[41][5]-8*6, g_Display_blank_output[41][7]);
#endif
	}
}

void Display_Measurement_Page(void) {
//  int i, j, Region_index = 1, lower_index = 1, upper_index = 3, key, lower_index1 = -1, upper_index1 = -1, err_no, redraw_screen = 0, active_length, key1, is_save = 0;
  int i, j, Region_index = 1, lower_index = 1, upper_index = 4, key, lower_index1 = -1, upper_index1 = -1, err_no, redraw_screen = 1, active_length, key1, is_save = 0, is_tail = 0, comfirm_yes_no, is_measure = 0, retry_times = 3, NewRegion_index = 1;
  char buf[10];
  int highlight = 0;
  struct Measure_tran temp_mea1;
  
  temp_mea1.blank_flag = -1;
  temp_mea1.sample_index = -1;
  temp_mea1.last_sample_No = 0;

Redraw_Measurement_Page:
//set the black backgreound
  TFT_Send_Command(gMeasure_page[0], sizeof(gMeasure_page[0]),0);
  TFT_Send_Command(gMeasure_page[1], sizeof(gMeasure_page[1]), 0);
  TFT_Send_Command(gMeasure_page[5], sizeof(gMeasure_page[5]), 0);
  TFT_Send_Command(gMeasure_page[6], sizeof(gMeasure_page[6]), 0);

	if (Region_index == 2) {
		switch (Assay_index) {
		  case 0://BCA
			  TFT_Send_Command(gMeasure_page[8], sizeof(gMeasure_page[8]), 0);
			  break;
		  case 1://Bradford
			  TFT_Send_Command(gMeasure_page[9], sizeof(gMeasure_page[9]), 0);
			  break;
		  case 2://Pierce
			  TFT_Send_Command(gMeasure_page[10], sizeof(gMeasure_page[10]), 0);
			  break;
		}
	}
	else {
		switch (Assay_index) {
		   case 0://BCA
			   TFT_Send_Command(gMeasure_page[2], sizeof(gMeasure_page[2]), 0);
			   break;
		   case 1://Bradford
			   TFT_Send_Command(gMeasure_page[3], sizeof(gMeasure_page[3]), 0);
			   break;
		   case 2://Pierce
			   TFT_Send_Command(gMeasure_page[4], sizeof(gMeasure_page[4]), 0);
			   break;
		}
	}

	if (Region_index <= 4) {
		switch (Region_index) {
		  case 1:
			  TFT_Send_Command(gMeasure_page[7], sizeof(gMeasure_page[7]), 0);  //blank highlight
			  TFT_Send_Command(gMeasure_page[13], sizeof(gMeasure_page[13]), 0);  //desc. of Blank
			  TFT_Send_Command(gMeasure_page[17], sizeof(gMeasure_page[17]), 0);  //icon blank
			  break;
		  case 2:
			  TFT_Send_Command(gMeasure_page[14], sizeof(gMeasure_page[14]), 0);  //desc. of Samples
			  TFT_Send_Command(gMeasure_page[18], sizeof(gMeasure_page[18]), 0);  //icon sample
			  break;
		  case 3:
			  highlight = 10;
			  TFT_Send_Command(gMeasure_page[15], sizeof(gMeasure_page[15]), 0);  //description of Data_xx
			  TFT_Send_Command(gMeasure_page[19], sizeof(gMeasure_page[19]), 0);  //icon sheet
			  TFT_Send_Command(gMeasure_page[11], sizeof(gMeasure_page[11]), 0);  //Data_XX highlight
			  break;
		  case 4:
			  TFT_Send_Command(gMeasure_page[16], sizeof(gMeasure_page[16]), 0);  //description of save & exit
			  TFT_Send_Command(gMeasure_page[20], sizeof(gMeasure_page[20]), 0);  //icon save
			  TFT_Send_Command(gMeasure_page[12], sizeof(gMeasure_page[12]), 0);  //save & exit highlight
			  sprintf(buf, "%02d", cur_sheet+1);
#ifndef LOCCUS_ODM
			  Show_LCD_IMG(gDigital_image, buf[0]-'0', 0xC0, 0x18);
			  Show_LCD_IMG(gDigital_image, buf[1]-'0', 0xB8, 0x18);
#else //for loccus
			  Show_LCD_IMG(gDigital_image, buf[0]-'0', 0x90, 0x05);
			  Show_LCD_IMG(gDigital_image, buf[1]-'0', 0x88, 0x05);
#endif
			  break;
		}

		if (!is_measure) {
			TFT_Send_Command(gMeasure_page[21], sizeof(gMeasure_page[21]), 0);  //======description======
		}
		else {
			TFT_Send_Command(gMeasure_page[22], sizeof(gMeasure_page[22]), 0);  //======output=======
			Display_measure_output1(&temp_mea1, Assay_index+1, temp_mea1.sample_index);
		}
	}
	else {
		j = (upper_index1 - (upper_index - Region_index));
		if (j >= 0) {
			TFT_Send_Command(gMeasure_page[22], sizeof(gMeasure_page[22]), 0);  //======output=======
			if (j > 0)
				Display_measure_output1(&temp_mea1, Assay_index+1, j - 1);  //display sample absorbance in output region
			else
				Display_measure_output1(&temp_mea1, 0, 0);  //display blank in output region
		}
		else {
			TFT_Send_Command(gMeasure_page[21], sizeof(gMeasure_page[21]), 0);
		}  	
	}

	sprintf(buf, "%02d", cur_sheet+1);
#ifndef LOCCUS_ODM
	Show_LCD_IMG(gDigital_image, buf[0]-'0', 0x30, 0xD8);
	Show_LCD_IMG(gDigital_image, buf[1]-'0', 0x28, 0xD8);
	Show_LCD_IMG(gDigital_image, buf[0]-'0'+highlight, 0xF8, 0x90);
	Show_LCD_IMG(gDigital_image, buf[1]-'0'+highlight, 0xF0, 0x90);
#else
	Show_LCD_IMG(gDigital_image, buf[0]-'0', 0x30, 0xD8);
	Show_LCD_IMG(gDigital_image, buf[1]-'0', 0x28, 0xD8);
	Show_LCD_IMG(gDigital_image, buf[0]-'0'+highlight, 0xF0, 0x90);
	Show_LCD_IMG(gDigital_image, buf[1]-'0'+highlight, 0xE8, 0x90);
#endif  //LOCCUS_ODM

	while(1) {
Sheet_redraw:
		if (redraw_screen==1) {
			for (i = 6, j = lower_index1; i < 11; i++, j++) {
				Show_LCD_IMG(gMeasure_page, 30, gMeasure_page[30][5], gMeasure_page[30][7]-(i-6)*24);
				if (i < (6+2+upper_index1) && temp_mea1.blank_flag > 0){
					if (lower_index1 > 0) {
						//             printf("sheet redraw type1\n");
						//             Delay100ms(3);
						sprintf(buf, "%02d", temp_mea1.sample_No[j-1]);
						if (NewRegion_index==i) {
							Show_LCD_IMG(gMeasure_page, 29, gMeasure_page[29][5], gMeasure_page[29][7]-(i-6-2)*24);  //sample_xx
							Show_LCD_IMG(gDigital_image+10, buf[0]-'0', gMeasure_page[31][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list first digit
							Show_LCD_IMG(gDigital_image+10, buf[1]-'0', gMeasure_page[32][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list second digit
						}
						else {
							Show_LCD_IMG(gMeasure_page, 26, gMeasure_page[26][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample_xx                                          
							Show_LCD_IMG(gDigital_image, buf[0]-'0', gMeasure_page[31][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list first digit
							Show_LCD_IMG(gDigital_image, buf[1]-'0', gMeasure_page[32][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list second digit
						}
					}
					else
						if (upper_index1<=3) {
							//printf("sheet redraw type2\n");
							if (i==6) {
								//                  Delay100ms(3);
								if (i==NewRegion_index) {
									TFT_Send_Command(gMeasure_page[27], sizeof(gMeasure_page[27]),0);  //Return highlight
									TFT_Send_Command(gMeasure_page[21], sizeof(gMeasure_page[21]),0);
								}
								else
									TFT_Send_Command(gMeasure_page[24], sizeof(gMeasure_page[24]),0);  //Return
								//                  printf("print return item\n");
								//getchar();
							}
							else
								if (i==7) {
									//                     Delay100ms(3);
									if (i==NewRegion_index) {
										TFT_Send_Command(gMeasure_page[28], sizeof(gMeasure_page[28]),0);  //blank highlight
										TFT_Send_Command(gMeasure_page[22], sizeof(gMeasure_page[22]),0);
									}
									else
										TFT_Send_Command(gMeasure_page[25], sizeof(gMeasure_page[25]),0);  //blank
									//                     printf("print blank item\n");
									//getchar();
								}
								else {
									sprintf(buf, "%02d", temp_mea1.sample_No[i-8]);
									if (NewRegion_index==i) {
										Show_LCD_IMG(gMeasure_page, 29, gMeasure_page[29][5], gMeasure_page[26][7]-(i-8)*24);  //sample_xx highlight
										Show_LCD_IMG(gDigital_image+10, buf[0]-'0', gMeasure_page[31][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list first digit
										Show_LCD_IMG(gDigital_image+10, buf[1]-'0', gMeasure_page[32][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list second digit
									}
									else {
										Show_LCD_IMG(gMeasure_page, 26, gMeasure_page[26][5], gMeasure_page[26][7]-(i-8)*24);  //sample_xx
										Show_LCD_IMG(gDigital_image, buf[0]-'0', gMeasure_page[31][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list first digit
										Show_LCD_IMG(gDigital_image, buf[1]-'0', gMeasure_page[32][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list second digit
									}
								}
						}
						else {
							//                 printf("sheet redraw type3\n");
							if (i==6) {
								//                   Delay100ms(3);
								if (i==NewRegion_index)
									Show_LCD_IMG(gMeasure_page, 28, gMeasure_page[28][5], gMeasure_page[28][7]+24);  //blank highlight
								else
									Show_LCD_IMG(gMeasure_page, 25, gMeasure_page[25][5], gMeasure_page[25][7]+24);  //blank
							}
							else {
								//                    Delay100ms(3);
								sprintf(buf, "%02d", temp_mea1.sample_No[i-7]);
								if (NewRegion_index==i) {
									Show_LCD_IMG(gMeasure_page, 29, gMeasure_page[29][5], gMeasure_page[26][7]-(i-8)*24);  //sample_xx highlight
									Show_LCD_IMG(gDigital_image+10, buf[0]-'0', gMeasure_page[31][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list first digit
									Show_LCD_IMG(gDigital_image+10, buf[1]-'0', gMeasure_page[32][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list second digit                    
								}
								else {
									Show_LCD_IMG(gMeasure_page, 26, gMeasure_page[26][5], gMeasure_page[26][7]-(i-8)*24);  //sample_xx
									Show_LCD_IMG(gDigital_image, buf[0]-'0', gMeasure_page[31][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list first digit
									Show_LCD_IMG(gDigital_image, buf[1]-'0', gMeasure_page[32][5], gMeasure_page[26][7]-(i-6-2)*24);  //sample list second digit
								}
							}
						}
				}
			}

			//      Delay100ms(20);
			if (upper_index1==(temp_mea1.sample_index+temp_mea1.blank_flag)) {		//redraw down arrow
				TFT_Send_Command(g_Msg_page[48], 12, 0);  //list area down key mask
			}
			else
				if (upper_index1 < (temp_mea1.sample_index+temp_mea1.blank_flag)) {
					TFT_Send_Command(g_Msg_page[47], 12, 0);  //list area down key
				}


				if (upper_index1 > 3) {		//redraw up arrow
					TFT_Send_Command(g_Msg_page[49], 12, 0);  //list area up key
				}
				else
					if (upper_index1 <= 3) {
						TFT_Send_Command(g_Msg_page[50], 12, 0);  //list area up key mask		  
					}

					/*if (is_measure) {
					TFT_Send_Command(gMeasure_page[24], sizeof(gMeasure_page[24]));
					Display_measure_output1(&temp_mea1, mode_index+1, temp_mea1.sample_index);
					}*/
					if (6 <= NewRegion_index && NewRegion_index <= 10) {
						j = (upper_index1 - (upper_index - Region_index));
						if (j >= 0) {
							TFT_Send_Command(gMeasure_page[22], sizeof(gMeasure_page[22]),0);
							if (j > 0)
								Display_measure_output1(&temp_mea1, Assay_index+1, j - 1);
							else
								Display_measure_output1(&temp_mea1, 0, 0);
						}
						else {
							TFT_Send_Command(gMeasure_page[21], sizeof(gMeasure_page[21]),0);
						}        
					}
					key = Null_key;
					redraw_screen = 0;
		}
		else {
#ifdef Keyboard_Emu
			key = getchar();
#else
#ifdef USE_KEY_QUEUE
			key = Get_key_queue(1);
#else
			key = User_key;
			User_key = Null_key;
#endif  //USE_KEY_QUEUE
#endif  //Keyboard_Emu
			if (key==Up_key || key==Down_key || key==Enter_key || key==Power_key) {
				active_length = (Region_index <= 4) ? 14:12;
			}
			else
				if (key==Refresh_key) {
					redraw_screen = 1;
					goto Redraw_Measurement_Page;
				}
				else
					continue;
		}  //redraw_screen==1

		if (key==Up_key) {
			NewRegion_index--;
			if (NewRegion_index < lower_index) {
				NewRegion_index = lower_index;

				if (lower_index==6) {
					if (upper_index1 > 3) {
						if (lower_index1 > 0)
							lower_index1--;
						upper_index1--;
						redraw_screen = 1;
					}
					else {
						upper_index1 = temp_mea1.sample_index+temp_mea1.blank_flag;
						if (upper_index1==3) {
							lower_index1 = 0;
							NewRegion_index = 10;
						}
						else
							if (upper_index1==4){
								lower_index1 = 0;
								NewRegion_index = 10;
							}
							else
								if (upper_index1 > 4) {
									lower_index1 = upper_index1 - 4;
									NewRegion_index = 10;
								}

								redraw_screen = 1;

					}
				}
			}
			if (NewRegion_index==1 && is_measure) {
				TFT_Send_Command(gMeasure_page[21], sizeof(gMeasure_page[21]),0);
				is_measure = 0;
			}
		}  //up_key
		else
			if (key==Down_key) {
				NewRegion_index++;
				if (NewRegion_index > upper_index) {
					NewRegion_index = upper_index;
					if (upper_index==10) {
						if (upper_index1 < (temp_mea1.sample_index+temp_mea1.blank_flag)) {
							if (upper_index1 > 3)
								lower_index1++;
							upper_index1++;
							//Region_index = NewRegion_index;
							redraw_screen = 1;
							//goto Sheet_redraw;
						}
						else {
							lower_index1 = 0;
							upper_index1 = 3;
							NewRegion_index = 6;
							//Region_index = NewRegion_index;
							redraw_screen = 1;
							//goto Sheet_redraw;
						}
					}
				}
				if (NewRegion_index==3 && is_measure) {
					TFT_Send_Command(gMeasure_page[21], sizeof(gMeasure_page[21]),0);
					is_measure = 0;
				}
			}  //Down_key
			else
				if (key==Power_key) {
					if (!is_Power_Down()) {
						redraw_screen = 1;
						goto Redraw_Measurement_Page;
					}
					else {
						outpw((uint32_t)&GPIOE->DOUT, inpw((uint32_t) & GPIOE->PIN) & ~(1 << 5));
						while(1);
					}
				}
				else
					if (key==Enter_key) {
						//measure_index = Region_index - 1;
						switch(Region_index) {
/*20130430 added by michael
measure blank*/
						  case 1:
							  TFT_Send_Command(gMain_menu_page[0], 12,0);  //background
							  TFT_Send_Command(g_Msg_page[0], 12, 0);
							  retry_times = 1;
							  while((err_no = Add_measurement(&temp_mea1, 0)) <  0 && --retry_times)
								  Delay100ms(20);
							  //err_no = 0;
							  if (!err_no) {
								  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
								  TFT_Send_Command(g_Msg_page[1], 12, 0);
								  Delay100ms(10);
								  lower_index1 = upper_index1 = 0;
							  }
							  else
								  if (err_no > 0) {
									  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background

									  TFT_Send_Command(g_Msg_page[2], 12,0);
									  TFT_Send_Command(g_Msg_page[3], 12,0);
									  TFT_Send_Command(g_Msg_page[4], 12,0);
									  TFT_Send_Command(g_Msg_page[9], 12,0);
									  TFT_Send_Command(g_Msg_page[7], 12,0);
									  TFT_Send_Command(g_Msg_page[10], 12,0);

									  comfirm_yes_no = 0;
#ifdef Keyboard_Emu
									  while ((key1 = getchar()) != Enter_key)
#else
									  while (1)
#endif
									  {
#ifdef Keyboard_Emu
#else
#ifdef USE_KEY_QUEUE
										  key1 = Get_key_queue(0);
#else
										  key1 = User_key;
										  User_key = Null_key;
#endif
										  if (key1==Enter_key)
											  break;
#endif
										  if (comfirm_yes_no==1) {
											  if (key1==Down_key) {
												  TFT_Send_Command(g_Msg_page[4], 12,0);
												  TFT_Send_Command(g_Msg_page[9], 12,0);
												  TFT_Send_Command(g_Msg_page[7], 12,0);
												  TFT_Send_Command(g_Msg_page[10], 12,0);
												  comfirm_yes_no = 0;
											  }
										  }
										  else 
											  if (key1==Up_key) {
												  TFT_Send_Command(g_Msg_page[5], 12,0);
												  TFT_Send_Command(g_Msg_page[8], 12,0);
												  TFT_Send_Command(g_Msg_page[6], 12,0);
												  TFT_Send_Command(g_Msg_page[11], 12,0);
												  comfirm_yes_no = 1;
											  }
									  }	

									  if (comfirm_yes_no==1) {
										  temp_mea1.blank_flag = -1;
										  TFT_Send_Command(gMain_menu_page[0], 12,0);  //background
										  TFT_Send_Command(g_Msg_page[0], 12,0);

										  err_no = Add_measurement(&temp_mea1, 0);
										  if (!err_no) {
											  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
											  TFT_Send_Command(g_Msg_page[1], 12,0);
										  }
										  else
											  if (err_no < 0) {
												  TFT_Send_Command(g_Msg_page[12], 12,0);

												  for (i = 0, j = 0; j < 3; j++)
													  if (temp_mea1.err_status & (0 << j)) {
														  if (i > 0) {
#ifndef LOCCUS_ODM
															  Show_LCD_IMG(g_Msg_page, 15, g_Msg_page[13][5]-i*8, g_Msg_page[15][7]);
#else
															  Show_LCD_IMG(g_Msg_page, 15, g_Msg_page[13][5]-i*8-40, g_Msg_page[15][7]);
#endif
															  i++;
														  }
														  sprintf(buf, "%02d, ", j+1);
#ifndef LOCCUS_ODM
														  Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Msg_page[13][5]-i*8, g_Msg_page[13][7]);
#else
														  Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Msg_page[13][5]-i*8-40, g_Msg_page[13][7]);
#endif
														  i++;
#ifndef LOCCUS_ODM
														  Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Msg_page[13][5]-i*8, g_Msg_page[13][7]);
#else
														  Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Msg_page[13][5]-i*8-40, g_Msg_page[13][7]);
#endif
														  i++;
													  }
											  }
											  Delay100ms(10);
									  }
								  }
								  else 
									  if (err_no < 0) {
										  printf("blank value error, please re-measure!\n");
										  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
										  TFT_Send_Command(g_Msg_page[12], 12, 0);

										  for (i = 0, j = 0; j < 3; j++)
											  if (temp_mea1.err_status & (0 << j)) {
												  if (i > 0) {
#ifndef LOCCUS_ODM
													  Show_LCD_IMG(g_Msg_page, 15, g_Msg_page[13][5]-i*8, g_Msg_page[15][7]);
#else
													  Show_LCD_IMG(g_Msg_page, 15, g_Msg_page[13][5]-i*8-40, g_Msg_page[15][7]);
#endif
													  i++;
												  }
												  sprintf(buf, "%02d, ", j+1);
#ifndef LOCCUS_ODM
												  Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Msg_page[13][5]-i*8, g_Msg_page[13][7]);
#else
												  Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Msg_page[13][5]-i*8-40, g_Msg_page[13][7]);
#endif
												  i++;
#ifndef LOCCUS_ODM
												  Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Msg_page[13][5]-i*8, g_Msg_page[13][7]);
#else
												  Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Msg_page[13][5]-i*8-40, g_Msg_page[13][7]);
#endif
												  i++;
											  }
											  Delay100ms(10);
									  }
                              redraw_screen = 1;
							  goto Redraw_Measurement_Page;
							  break;

/*20130503 added by michael
measure sample*/
                          case 2:
							  if (temp_mea1.blank_flag > 0) {
								  i = temp_mea1.sample_index+temp_mea1.blank_flag;
								  if (i < MAX_Samples) {
									  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
									  TFT_Send_Command(g_Msg_page[16], 12, 0);

									  retry_times = 1;
									  while((err_no = Add_measurement(&temp_mea1, Assay_index+1)) <  0 && --retry_times)
										  Delay100ms(20);
									  i = temp_mea1.sample_index+temp_mea1.blank_flag;
									  if (!err_no && i > 0 && i <= 3) {
										  //sprintf(buf2, "%s%d", Measure_disp_data[7+i].content, temp_mea1.sample_No[temp_mea1.sample_index]);
										  //Show_text1(Measure_disp_data[7+i].disp_x, Measure_disp_data[7+i].disp_y, buf2);
										  upper_index1++;
									  }
									  if (!err_no) {
										  sprintf(buf, "%02d", temp_mea1.sample_No[temp_mea1.sample_index]);
										  TFT_Send_Command(g_Msg_page[17], 12, 0);
#ifndef LOCCUS_ODM
										  Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Msg_page[18][5], g_Msg_page[18][7]);
										  Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Msg_page[19][5], g_Msg_page[19][7]);
#else
										  Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Msg_page[18][5]-8, g_Msg_page[18][7]);
										  Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Msg_page[19][5]-8, g_Msg_page[19][7]);
#endif
										  is_measure = 1;

										  Delay100ms(10);
									  }
									  else
										  if (err_no < 0) {
											  switch (err_no) {
                                                case -2:
													printf("A562 value error, please re-measure!, code: %d\n", err_no);
													break;
												case -3:
													printf("A595 value error, please re-measure!, code: %d\n", err_no);
													break;
												case -4:
													printf("A660 value error, please re-measure!, code: %d\n", err_no);
													break;
											  }
											  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
											  TFT_Send_Command(g_Msg_page[20], 12, 0);
											  Delay100ms(10);
										  }
								  }
								  else {
									  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
									  TFT_Send_Command(g_Msg_page[21], 12, 0);  //Warning: more than limited quantity of records!
									  Delay100ms(10);			 
								  }
							  }
							  else {
								  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
								  TFT_Send_Command(g_Msg_page[22], 12, 0);  //Warning: please measure blank first!
								  Delay100ms(10);
								  printf("Warning: please measure blank first!\n");
							  }
							  redraw_screen = 1;
							  goto Redraw_Measurement_Page;
							  break;
/*20130503 added by michael
enter data sheet*/
						  case 3:
							  if (temp_mea1.blank_flag > 0) {
								  lower_index = 6;
								  upper_index = lower_index+temp_mea1.sample_index+temp_mea1.blank_flag+1;
								  if (upper_index > 10)
									  upper_index = 10;
								  NewRegion_index = 6;
							  }
							  break;
/*20130503 added by michael
save & exit*/
						  case 4:
							  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
							  TFT_Send_Command(g_Msg_page[23], 12,0);  //Exit to "Main Menu"?
							  TFT_Send_Command(g_Msg_page[24], 12,0);  //=========================
							  is_save = 0;
							  TFT_Send_Command(g_Msg_page[26], 12,0);  //save button highlight
							  TFT_Send_Command(g_Msg_page[27], 12,0);  //enter key
							  TFT_Send_Command(g_Msg_page[29], 12,0);  //exit button
							  TFT_Send_Command(g_Msg_page[32], 12,0);  //enter key black mask
							  TFT_Send_Command(g_Msg_page[33], 12,0);  //cancel button
							  TFT_Send_Command(g_Msg_page[36], 12,0);  //enter key black mask

							  while (1) {

#ifdef Keyboard_Emu
								  key1 = getchar();
#else
#ifdef USE_KEY_QUEUE
								  key1 = Get_key_queue(0);
#else
								  key1 = User_key;
								  User_key = Null_key;
#endif
#endif
								  switch (key1) {
						            case Enter_key:
										if (is_save==1) {  //Exit
											return;
										}
										else
											if (is_save==0) {  //Save
												i = temp_mea1.sample_index+temp_mea1.blank_flag;
												if (i > 0) {
													if (Store_sheet(&temp_mea1, Assay_index+1, sheet_info+cur_sheet, cur_sheet)==-1) {
														printf("store flash error\n");
														return;
													}

													if (head_sheet==-1) {
														head_sheet = 0;
														tail_sheet = 1;
													}
													else {
														tail_sheet++;
														if (tail_sheet==MAX_Sheets) {
															head_sheet = 1;
															tail_sheet = 0;
														}
														else
															if (head_sheet==tail_sheet) {
																head_sheet++;
															}

													}
													cur_sheet = tail_sheet;
													Update_sheet_index(cur_sheet, head_sheet, tail_sheet);
													return;
												}
												else
													return;

											}
											else {  //Cancel
												redraw_screen = 1;
												goto Redraw_Measurement_Page;
											}

										break;
									case Up_key:
										if (is_save > 0)
											is_save--;
										break;
									case Down_key:
										if (is_save < 2)
											is_save++;
										break;
								  }


								  if (key1==Up_key || key1==Down_key) {
									  switch (is_save) {
						                case 0:
											TFT_Send_Command(g_Msg_page[26], 12,0);  //save button highlight
											TFT_Send_Command(g_Msg_page[27], 12,0);  //enter key
											TFT_Send_Command(g_Msg_page[29], 12,0);  //exit button
											TFT_Send_Command(g_Msg_page[32], 12,0);  //enter key black mask
											TFT_Send_Command(g_Msg_page[33], 12,0);  //cancel button
											TFT_Send_Command(g_Msg_page[36], 12,0);  //enter key black mask
											break;
										case 1:
											TFT_Send_Command(g_Msg_page[25], 12,0);  //save button
											TFT_Send_Command(g_Msg_page[28], 12,0);  //enter key black mask
											TFT_Send_Command(g_Msg_page[30], 12,0);  //exit button highlight
											TFT_Send_Command(g_Msg_page[31], 12,0);  //enter key
											TFT_Send_Command(g_Msg_page[33], 12,0);  //cancel button
											TFT_Send_Command(g_Msg_page[36], 12,0);  //enter key black mask
											break;
										case 2:
											TFT_Send_Command(g_Msg_page[25], 12,0);  //save button
											TFT_Send_Command(g_Msg_page[28], 12,0);  //enter key black mask
											TFT_Send_Command(g_Msg_page[29], 12,0);  //exit button
											TFT_Send_Command(g_Msg_page[32], 12,0);  //enter key black mask
											TFT_Send_Command(g_Msg_page[34], 12,0);  //cancel button
											TFT_Send_Command(g_Msg_page[35], 12,0);  //enter key
											break;
									  }
								  }
							  }
							  break;

						  case 6:
						  case 7:
		                  case 8:
		                  case 9:
		                  case 10:
							  measure_index = (upper_index1 - (upper_index - Region_index));
							  printf ("lower_index1=%d, upper_index1: %d, measure_index: %d, %d\n", lower_index1, upper_index1, measure_index, temp_mea1.sample_index+temp_mea1.blank_flag);
							  if (measure_index > 0) {
								  comfirm_yes_no = 0;
								  TFT_Send_Command(gMain_menu_page[0], 12,0);  //background
								  TFT_Send_Command(g_Msg_page[37], 12,0);  //sample_xx: Delete the selected sample?
								  sprintf(buf, "%02d", temp_mea1.sample_No[measure_index-1]);
#ifndef LOCCUS_ODM
								  Show_LCD_IMG(gDigital_image, buf[0]-'0', 0xA8, 0xCA);
								  Show_LCD_IMG(gDigital_image, buf[1]-'0', 0xA0, 0xCA);
#else
								  Show_LCD_IMG(gDigital_image, buf[0]-'0', 0xA0, 0xCA);
								  Show_LCD_IMG(gDigital_image, buf[1]-'0', 0x98, 0xCA);
#endif
								  TFT_Send_Command(g_Msg_page[38], 12,0);  //===========================
								  TFT_Send_Command(g_Msg_page[39], 12,0);  //Yes button
								  TFT_Send_Command(g_Msg_page[42], 12,0);  //Enter_key mask black
								  TFT_Send_Command(g_Msg_page[44], 12,0);  //No button highlight
								  TFT_Send_Command(g_Msg_page[45], 12,0);  //Enter_key

#ifdef Keyboard_Emu
								  while ((key1 = getchar()) != Enter_key) {
#else
								  while (1) {
#ifdef USE_KEY_QUEUE
									  key1 = Get_key_queue(0);
#else
									  key1 = User_key;
									  User_key = Null_key;
#endif
#endif
									  if (key1==Enter_key)
										  break;
									  if (comfirm_yes_no==1) {
										  if (key1==Down_key) {
											  comfirm_yes_no = 0;
											  TFT_Send_Command(g_Msg_page[39], 12,0);  //Yes button
											  TFT_Send_Command(g_Msg_page[42], 12,0);  //Enter_key mask black
											  TFT_Send_Command(g_Msg_page[44], 12,0);  //No button highlight
											  TFT_Send_Command(g_Msg_page[45], 12,0);  //Enter_key
										  }
									  }
									  else 
										  if (key1==Up_key) {
											  comfirm_yes_no = 1;
											  TFT_Send_Command(g_Msg_page[40], 12,0);  //Yes button
											  TFT_Send_Command(g_Msg_page[41], 12,0);  //Enter_key highlight
											  TFT_Send_Command(g_Msg_page[43], 12,0);  //No button
											  TFT_Send_Command(g_Msg_page[46], 12,0);  //Enter_key mask black
										  }
								  }

								  if (comfirm_yes_no==1) {
									  is_tail = Del_measurement(&temp_mea1, measure_index-1);
								  }
								  if (upper_index1 >  (temp_mea1.sample_index+temp_mea1.blank_flag)) {
									  if (lower_index1 > 0) {
										  lower_index1--;
										  upper_index1--;
										  if (upper_index1==4 && Region_index==6)
											  NewRegion_index = 7;
									  }
									  else
										  if (upper_index1 <=3) {
											  upper_index1--;
											  upper_index--;
											  if (is_tail)
												  NewRegion_index--;
										  }
										  else {
											  upper_index1--;
											  if (upper_index1==3 && Region_index==7)
												  NewRegion_index = 8;
										  }
								  }
								  else
									  if (upper_index1==(temp_mea1.sample_index+temp_mea1.blank_flag)) {
										  //TFT_Send_Command(g_Msg_page[48], 12,0);  //list area down key mask
									  }
									  else
										  if (upper_index1 < (temp_mea1.sample_index+temp_mea1.blank_flag)) {
										  }

								  redraw_screen = 1;
								  goto Redraw_Measurement_Page;
							  }
							  else
								  if (measure_index < 0 && Region_index==6) {
									  lower_index = 1;
									  upper_index = 4;
									  NewRegion_index = 3;
								  }
			                  break;


						}  //switch(Region_index)
					}  //Enter_key



					if (NewRegion_index != Region_index || redraw_screen==1) {
						switch (Region_index) {
						  case 1:
							  TFT_Send_Command(gMeasure_page[1], sizeof(gMeasure_page[1]),0);
							  break;
						  case 2:
							  switch (Assay_index) {
						        case 0://BCA
									TFT_Send_Command(gMeasure_page[2], sizeof(gMeasure_page[2]),0);
									break;
								case 1://Bradford
									TFT_Send_Command(gMeasure_page[3], sizeof(gMeasure_page[3]),0);
									break;
								case 2://Pierce
									TFT_Send_Command(gMeasure_page[4], sizeof(gMeasure_page[4]),0);
									break;
							  }
							  break;
						  case 3:
							  sprintf(buf, "%02d", cur_sheet+1);
							  TFT_Send_Command(gMeasure_page[5], sizeof(gMeasure_page[5]),0);
#ifndef LOCCUS_ODM
							  Show_LCD_IMG(gDigital_image, buf[0]-'0', 0xF8, 0x90);
							  Show_LCD_IMG(gDigital_image, buf[1]-'0', 0xF0, 0x90);
#else
							  Show_LCD_IMG(gDigital_image, buf[0]-'0', 0xF0, 0x90);
							  Show_LCD_IMG(gDigital_image, buf[1]-'0', 0xE8, 0x90);
#endif
							  break;
						  case 4:
							  TFT_Send_Command(gMeasure_page[6], sizeof(gMeasure_page[6]),0);
							  break;
						  case 6:
							  TFT_Send_Command(gMeasure_page[24], sizeof(gMeasure_page[24]),0);  //Return
							  if (6 < NewRegion_index)
								  TFT_Send_Command(gMeasure_page[22], sizeof(gMeasure_page[22]),0);

							  break;
						}

						switch(NewRegion_index) {
						  case 1:
							  TFT_Send_Command(gMeasure_page[21], sizeof(gMeasure_page[21]),0);
							  TFT_Send_Command(gMeasure_page[13], sizeof(gMeasure_page[13]),0);
							  TFT_Send_Command(gMeasure_page[17], sizeof(gMeasure_page[17]),0);
							  TFT_Send_Command(gMeasure_page[7], sizeof(gMeasure_page[7]),0);
							  break;

						  case 2:
							  //if (!is_measure) {
							  TFT_Send_Command(gMeasure_page[14], sizeof(gMeasure_page[14]),0);
							  TFT_Send_Command(gMeasure_page[18], sizeof(gMeasure_page[18]),0);
							  switch (Assay_index) {
						         case 0://BCA
									 TFT_Send_Command(gMeasure_page[8], sizeof(gMeasure_page[8]),0);
									 break;
								 case 1://Bradford
									 TFT_Send_Command(gMeasure_page[9], sizeof(gMeasure_page[9]),0);
									 break;
								 case 2://Pierce
									 TFT_Send_Command(gMeasure_page[10], sizeof(gMeasure_page[10]),0);
									 break;
							  }
							  /*if (is_measure) {
								  Display_measure_output1(&temp_mea1, Assay_index+1, temp_mea1.sample_index);
							  }*/
							  break;

						  case 3:
							  TFT_Send_Command(gMeasure_page[23], sizeof(gMeasure_page[23]),0);  //clear the right screen
							  TFT_Send_Command(gMeasure_page[15], sizeof(gMeasure_page[15]),0);
							  TFT_Send_Command(gMeasure_page[19], sizeof(gMeasure_page[19]),0);
							  TFT_Send_Command(gMeasure_page[11], sizeof(gMeasure_page[11]),0);

							  sprintf(buf, "%02d", cur_sheet+1);  
#ifndef LOCCUS_ODM
							  Show_LCD_IMG(gDigital_image, buf[0]-'0'+10, 0xF8, 0x90);
							  Show_LCD_IMG(gDigital_image, buf[1]-'0'+10, 0xF0, 0x90);
							  Show_LCD_IMG(gDigital_image, buf[0]-'0', 0x78, 0x30);
							  Show_LCD_IMG(gDigital_image, buf[1]-'0', 0x70, 0x30);
#else //for loccus
							  Show_LCD_IMG(gDigital_image, buf[0]-'0'+10, 0xF0, 0x90);
							  Show_LCD_IMG(gDigital_image, buf[1]-'0'+10, 0xE8, 0x90);
							  Show_LCD_IMG(gDigital_image, buf[0]-'0', 232, 27);
							  Show_LCD_IMG(gDigital_image, buf[1]-'0', 224, 27);
#endif
							  break;

						  case 4:
							  TFT_Send_Command(gMeasure_page[16], sizeof(gMeasure_page[16]),0);
							  TFT_Send_Command(gMeasure_page[20], sizeof(gMeasure_page[20]),0);
							  TFT_Send_Command(gMeasure_page[12], sizeof(gMeasure_page[12]),0);
							  sprintf(buf, "%02d", cur_sheet+1);
#ifndef LOCCUS_ODM
							  Show_LCD_IMG(gDigital_image, buf[0]-'0', 0xC0, 0x18);
							  Show_LCD_IMG(gDigital_image, buf[1]-'0', 0xB8, 0x18);
#else //for loccus
							  Show_LCD_IMG(gDigital_image, buf[0]-'0', 0x90, 0x05);
							  Show_LCD_IMG(gDigital_image, buf[1]-'0', 0x88, 0x05);
#endif
							  break;

						  case 6:
							  j = (upper_index1 - (upper_index - NewRegion_index));
							  if (j < 0) {
								  TFT_Send_Command(gMeasure_page[21], sizeof(gMeasure_page[21]),0);
								  TFT_Send_Command(gMeasure_page[23], sizeof(gMeasure_page[23]),0);
								  TFT_Send_Command(gMeasure_page[27], sizeof(gMeasure_page[27]),0);
							  }

							  Region_index = NewRegion_index;
							  redraw_screen = 1;
							  goto Sheet_redraw;
							  break;

						  default:
							  Region_index = NewRegion_index;
							  redraw_screen = 1;
							  goto Sheet_redraw;
							  break;
						}

						Region_index = NewRegion_index;
					}
					else {
						/*if (is_measure && NewRegion_index==2) {
						Display_measure_output1(&temp_mea1, mode_index+1, temp_mea1.sample_index);
						is_measure = 0;
						}*/
					}



  }  //while
}


/*20130430 added by michael
Display main menu page*/
void Display_Main_Menu_Page(void) {
	int i, row = 16, j, col = 0, total_pixels, width, height, key, Region_index = 12, lower_index = 12, upper_index = 16, NewRegion_index = 12;
	char *pstr;

//#if defined(DEF_ODM) && (!defined(TIANGEN_ODM) && !defined(LOCCUS_ODM) && !defined(KASVI_ODM))
	upper_index--;
//#endif;

Main_menu:
	for (i = 0; i < 12; i++) {
		while (TFT_Busy)
			Delay100ms(1);
//#if !defined(DEF_ODM) || (defined(DEF_ODM) && defined(TIANGEN_ODM)) || (defined(DEF_ODM) && defined(LOCCUS_ODM))  ||  (defined(DEF_ODM) && defined(KASVI_ODM))
//		TFT_Send_Command(gMain_menu_page[i], sizeof(gMain_menu_page[i]),0);
//#else
		if (i != 7)
			TFT_Send_Command(gMain_menu_page[i], sizeof(gMain_menu_page[i]),0);
//#endif
		/*#ifdef TFT_DEBUG
		Delay100ms(20);
		getchar();
		#endif*/
	}

	/*if (mode_index < 0) {
		mode_index = 0;
	} */
	//getchar();
	//printf("press any key to print mode: %d\n", mode_index);
	/*while (TFT_Busy)
		Delay100ms(1);
	TFT_Send_Command(gMain_menu_page[mode_index+17], sizeof(gMain_menu_page[mode_index+17]),0);
	while (TFT_Busy)
		Delay100ms(1);
	TFT_Send_Command(gMain_menu_page[mode_index+17], sizeof(gMain_menu_page[mode_index+17]),0);*/

	if (menu_index >= 0) {
		Region_index = menu_index + 12;
	}
	//getchar();
	//printf("press any key to highlight focus: %d\n", Region_index);
	Delay100ms(2);
	TFT_Send_Command(gMain_menu_page[Region_index], sizeof(gMain_menu_page[Region_index]),0);
	TFT_Send_Command(gMain_menu_page[Region_index], sizeof(gMain_menu_page[Region_index]),0);

	/*while (key != Enter_key) {
	key = Get_key_queue(0);
	}*/

#ifdef Keyboard_Emu
	while ((key = getchar()) != Enter_key) {
#else
	while (1) {
#ifdef USE_KEY_QUEUE
		key = Get_key_queue(1);
    if (key)
      printf("key id: %d\n", key);
#else
		key = User_key;
		User_key = Null_key;
#endif
		if (key==Enter_key)
			break;
		else
			if (key==Null_key)
				continue;
			else
				if (key==Refresh_key)
					goto Main_menu;
#endif

		switch (key) {
		  case Up_key:
			  TFT_Send_Command(gMain_menu_page[23], sizeof(gMain_menu_page[23]),0);
			  Delay100ms(2);
			  TFT_Send_Command(gMain_menu_page[23+1], sizeof(gMain_menu_page[23+1]),0);
			  break;
		  case Down_key:
			  TFT_Send_Command(gMain_menu_page[25], sizeof(gMain_menu_page[25]),0);
			  Delay100ms(2);
			  TFT_Send_Command(gMain_menu_page[25+1], sizeof(gMain_menu_page[25+1]),0);
			  break;
		  case Power_key:
			  //TFT_Send_Command(gMain_menu_page[Region_index-9], sizeof(gMain_menu_page[Region_index-9]));
			  if (!is_Power_Down()) {
				  menu_index = Region_index - 12;
				  goto Main_menu;
			  }
			  else {
				  outpw((uint32_t)&GPIOE->DOUT, inpw((uint32_t) & GPIOE->PIN) & ~(1 << 5));
				  while(1);
			  }
			  break;
		}

		TFT_Send_Command(gMain_menu_page[Region_index-9], sizeof(gMain_menu_page[Region_index-9]),0);
		if (key==Up_key) {
			Region_index = Region_index - 1;
			if (Region_index < lower_index) {
				Region_index = upper_index;
			}
		}
		else
			if (key==Down_key) {
				Region_index = Region_index + 1;
				if (Region_index > upper_index) {
					Region_index = lower_index;
				}
			}

		TFT_Send_Command(gMain_menu_page[Region_index], sizeof(gMain_menu_page[Region_index]),0);
			//Region_index = NewRegion_index;
			//if (NewRegion_index != Region_index && NewRegion_index != 13) {

			//Delay100ms(3);

			//Region_index = NewRegion_index;
			//}

			//printf("SCAN MAIN MENU PAGE KEY!\n");
			//getchar();
	}

	menu_index = Region_index - 12;
	TFT_Send_Command(gMain_menu_page[21], sizeof(gMain_menu_page[21]),0);
	Delay100ms(2);
	TFT_Send_Command(gMain_menu_page[21+1], sizeof(gMain_menu_page[21+1]),0);
#ifdef APP_DEBUG
	printf("Main menu: Enter\n");
#endif
	return;
}


/*20130430 added by michael
is_Power_Down*/

int is_Power_Down()
{
	int comfirm_yes_no = 0, key1;

	TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background              
	TFT_Send_Command(g_Msg_page[51], 12,0);  //shutdown now?
	TFT_Send_Command(g_Msg_page[3], 12,0);   //=======
	TFT_Send_Command(g_Msg_page[4], 12,0);  //Yes button without highlight
	TFT_Send_Command(g_Msg_page[9], 12,0);  //No button with highlight
	TFT_Send_Command(g_Msg_page[7], 12,0);  //Enter key mask for  Yes button
	TFT_Send_Command(g_Msg_page[10], 12,0);  //Enter key  for No button

	comfirm_yes_no = 0;
	DrvGPIO_DisableInt(GPC, 0);
	DrvGPIO_EnableInt(GPC, 0, IO_FALLING, MODE_EDGE);
#ifdef Keyboard_Emu
	while ((key1 = getchar()) != Enter_key) {
#else
	while (1) {
#ifdef USE_KEY_QUEUE
		key1 = Get_key_queue(0);
#else
		key1 = User_key;
		User_key = Null_key;
#endif
		if (key1==Enter_key)
			break;
		else
			if (key1==Power_key) {
				comfirm_yes_no = 0;
				break;
			}
#endif
			if (comfirm_yes_no==1) {
				if (key1==Down_key) {
					TFT_Send_Command(g_Msg_page[4], 12,0);
					TFT_Send_Command(g_Msg_page[9], 12,0);
					TFT_Send_Command(g_Msg_page[7], 12,0);
					TFT_Send_Command(g_Msg_page[10], 12,0);
					comfirm_yes_no = 0;
				}
			}
			else 
				if (key1==Up_key) {
					TFT_Send_Command(g_Msg_page[5], 12,0);
					TFT_Send_Command(g_Msg_page[8], 12,0);
					TFT_Send_Command(g_Msg_page[6], 12,0);
					TFT_Send_Command(g_Msg_page[11], 12,0);
					comfirm_yes_no = 1;
				}
	}

	TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background 
	if (!comfirm_yes_no) {
		DrvGPIO_DisableInt(GPC, 0);
		DrvGPIO_EnableInt(GPC, 0, IO_RISING, MODE_EDGE);
		return 0;
	}
	else {
		return 1;
	}
}


void Display_Time_Setup() {

}


void Display_Show_And_Print_Page()
{
	int i, j, lower_index = 3, upper_index = -1, key, Region_index = 3, lower_index1 = -1, upper_index1 = -1, total_sheets = 0, show_back = 1, NewRegion_index = 3;
	int circular_index[MAX_Sheets] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
	char buf[10];
	int change_highlight = 0;
	union MaestroDate curdate;
	struct Measure_tran temp_mea1;

	printf("sheet circular index %d %d\n", head_sheet, tail_sheet);
	total_sheets = 0;
	if (head_sheet < tail_sheet) {
		for (i = 0; i < MAX_Sheets; i++)
			if (i < tail_sheet) {
				circular_index[i] = tail_sheet - i;
				total_sheets++;
			}
			else {
				circular_index[i] = -1;
			}
	}
	else 
		if (head_sheet > tail_sheet) {
			for (j = 0, i = tail_sheet - 1; i >= 0; i--)
				circular_index[j++] = i + 1;
			for (i = MAX_Sheets-1; i >= tail_sheet; i--)
				circular_index[j++] = i+1;
			total_sheets = MAX_Sheets;
		}

	if (total_sheets==0) {
		lower_index1 = -1;
		upper_index1 = -1;
		upper_index = lower_index;
	}
	else
		if (total_sheets < MAX_Page_Sheets) {
			lower_index1 = 0;
			upper_index1 = total_sheets - 1;
			upper_index = lower_index + (upper_index1-lower_index1+1);
		}
		else {
			lower_index1 = 0;
			upper_index1 = MAX_Page_Sheets - 1;
			upper_index = 9;
		}

show_and_print:
	TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
	TFT_Send_Command(g_Show_Print_page[0], 12, 0);

	if (show_back==1 && upper_index1==(MAX_Page_Sheets - 1))
		upper_index1--;
	for (i = 0 ; i < MAX_Sheets; i++)
		printf("%d ", circular_index[i]);
	printf("\n");


show_and_print_menu:
	if (MAX_Sheets > (MAX_Page_Sheets-1) && (tail_sheet < head_sheet) || ((head_sheet < tail_sheet) && tail_sheet > (MAX_Page_Sheets-1))) {  //draw down arrow
		if (upper_index1 < (total_sheets-1)) {
			TFT_Send_Command(g_Show_Print_page[25], 12, 0);  //draw down arrow
		}
		else {
			TFT_Send_Command(g_Show_Print_page[26], 12, 0);  //remove down arrow
		}
	}
	if ((upper_index1-lower_index1)==(MAX_Page_Sheets-1)) {  //draw up arrow
		TFT_Send_Command(g_Show_Print_page[27], 12, 0);  //draw up arrow
	}
	else {
		TFT_Send_Command(g_Show_Print_page[28], 12, 0);  //remove up arrow
	}
	printf("lower_index1: %d, upper_index1: %d, Region_index:%d, show_back: %d\n", lower_index1, upper_index1, Region_index, show_back);
	for (i = lower_index1; 0 <= i && i <= upper_index1; i++) {
		if (circular_index[i]==-1) {
			break;
		}
		else {
#ifdef USE_DATA_FLASH
			memset(&sheet_info[circular_index[i]-1], 0, sizeof(struct Measure_sheet));
			if (Load_sheet(&temp_mea1, &sheet_info[circular_index[i]-1], circular_index[i]-1)==-1) {
				printf("load flash error\n");
				return;
			}
			j = sheet_info[circular_index[i]-1].sheet_type - 1;
			curdate = sheet_info[circular_index[i]-1].sheet_date;
		
			if (!curdate.PackDate.Year)
				curdate.PackDate.Year = 2000;
			sprintf(buf2, "Data_%02d  %02d%02d%02d %02d%02d %s", circular_index[i], (curdate.PackDate.Year-2000), curdate.PackDate.Month, curdate.PackDate.Day, \
				curdate.PackDate.Hour, curdate.PackDate.Minute);
			printf("kkk%s, sheet number: %d\n", buf2, j);
			if (((3+i-lower_index1+show_back)==Region_index && change_highlight==0) ||
				((3+i-lower_index1+show_back)==NewRegion_index && change_highlight==1)) {
					Show_LCD_IMG(g_Show_Print_page, 4, g_Show_Print_page[4][5], 240-24*(3+i-lower_index1+show_back+1));  //Data_XX

					sprintf(buf, "%02d", circular_index[i]);
#ifndef LOCCUS_ODM
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[5][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[6][5], 240-24*(3+i-lower_index1+show_back+1));
#else
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[5][5]-8, 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[6][5]-8, 240-24*(3+i-lower_index1+show_back+1));
#endif
#ifndef LOCCUS_ODM
					sprintf(buf, "%02d", (curdate.PackDate.Year-2000));
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[7][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[8][5], 240-24*(3+i-lower_index1+show_back+1));

					sprintf(buf, "%02d", curdate.PackDate.Month);
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[9][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[10][5], 240-24*(3+i-lower_index1+show_back+1));

					sprintf(buf, "%02d", curdate.PackDate.Day);
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[11][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[12][5], 240-24*(3+i-lower_index1+show_back+1));
#else
					sprintf(buf, "%02d", curdate.PackDate.Day);
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[7][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[8][5], 240-24*(3+i-lower_index1+show_back+1));

					sprintf(buf, "%02d", curdate.PackDate.Month);
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[9][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[10][5], 240-24*(3+i-lower_index1+show_back+1));

					sprintf(buf, "%02d", (curdate.PackDate.Year-2000));
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[11][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[12][5], 240-24*(3+i-lower_index1+show_back+1));
#endif
#ifndef LOCCUS_ODM
					sprintf(buf, "%02d", curdate.PackDate.Hour);
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[13][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[14][5], 240-24*(3+i-lower_index1+show_back+1));
#else
					sprintf(buf, "%02d", curdate.PackDate.Hour);
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[13][5]+8, 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[14][5]+8, 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gChar_image, 1, gChar_image[0][5], 240-24*(3+i-lower_index1+show_back+1));
#endif
					sprintf(buf, "%02d", curdate.PackDate.Minute);
					Show_LCD_IMG(gDigital_image+10, *buf-'0', g_Show_Print_page[15][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image+10, *(buf+1)-'0', g_Show_Print_page[16][5], 240-24*(3+i-lower_index1+show_back+1));
					switch (j) {
					   case 0:
						   Show_LCD_IMG(g_Show_Print_page, 18, g_Show_Print_page[18][5], 240-24*(3+i-lower_index1+show_back+1));
						   break;
					   case 1:
						   Show_LCD_IMG(g_Show_Print_page, 20, g_Show_Print_page[20][5], 240-24*(3+i-lower_index1+show_back+1));
						   break;
					   case 2:
						   Show_LCD_IMG(g_Show_Print_page, 22, g_Show_Print_page[22][5], 240-24*(3+i-lower_index1+show_back+1));
						   break;
					   case 3:
						   Show_LCD_IMG(g_Show_Print_page, 24, g_Show_Print_page[24][5], 240-24*(3+i-lower_index1+show_back+1));
						   break;
					}
			}
			else
				if ((change_highlight==1 && (3+i-lower_index1+show_back)==Region_index) || change_highlight==0) {
					Show_LCD_IMG(g_Show_Print_page, 3, g_Show_Print_page[3][5], 240-24*(3+i-lower_index1+show_back+1));  //Data_XX
					sprintf(buf, "%02d", circular_index[i]);
			
#ifndef LOCCUS_ODM
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[5][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[6][5], 240-24*(3+i-lower_index1+show_back+1));
#else
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[5][5]-8, 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[6][5]-8, 240-24*(3+i-lower_index1+show_back+1));
#endif

#ifndef LOCCUS_ODM
					sprintf(buf, "%02d", (curdate.PackDate.Year-2000));
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[7][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[8][5], 240-24*(3+i-lower_index1+show_back+1));

					sprintf(buf, "%02d", curdate.PackDate.Month);
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[9][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[10][5], 240-24*(3+i-lower_index1+show_back+1));

					sprintf(buf, "%02d", curdate.PackDate.Day);
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[11][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[12][5], 240-24*(3+i-lower_index1+show_back+1));
#else
					sprintf(buf, "%02d", curdate.PackDate.Day);
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[7][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[8][5], 240-24*(3+i-lower_index1+show_back+1));

					sprintf(buf, "%02d", curdate.PackDate.Month);
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[9][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[10][5], 240-24*(3+i-lower_index1+show_back+1));

					sprintf(buf, "%02d", (curdate.PackDate.Year-2000));
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[11][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[12][5], 240-24*(3+i-lower_index1+show_back+1));
#endif

#ifndef LOCCUS_ODM
					sprintf(buf, "%02d", curdate.PackDate.Hour);
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[13][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[14][5], 240-24*(3+i-lower_index1+show_back+1));
#else
					sprintf(buf, "%02d", curdate.PackDate.Hour);
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[13][5]+8, 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[14][5]+8, 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gChar_image, 0, gChar_image[0][5], 240-24*(3+i-lower_index1+show_back+1));
#endif
					sprintf(buf, "%02d", curdate.PackDate.Minute);
					Show_LCD_IMG(gDigital_image, *buf-'0', g_Show_Print_page[15][5], 240-24*(3+i-lower_index1+show_back+1));
					Show_LCD_IMG(gDigital_image, *(buf+1)-'0', g_Show_Print_page[16][5], 240-24*(3+i-lower_index1+show_back+1));

					switch (j) {
					   case 0:
						   Show_LCD_IMG(g_Show_Print_page, 17, g_Show_Print_page[17][5], 240-24*(3+i-lower_index1+show_back+1));
						   break;
					   case 1:
						   Show_LCD_IMG(g_Show_Print_page, 19, g_Show_Print_page[19][5], 240-24*(3+i-lower_index1+show_back+1));
						   break;
					   case 2:
						   Show_LCD_IMG(g_Show_Print_page, 21, g_Show_Print_page[21][5], 240-24*(3+i-lower_index1+show_back+1));
						   break;
					   case 3:
						   Show_LCD_IMG(g_Show_Print_page, 23, g_Show_Print_page[23][5], 240-24*(3+i-lower_index1+show_back+1));
						   break;
					}
				}
#else
#endif
		}
	}

	if (change_highlight==1) {
		Region_index = NewRegion_index;
		change_highlight = 0;
	}

	if (show_back==1) {
		if (Region_index <= 3) {
			TFT_Send_Command(g_Show_Print_page[2], 12, 0);
		}
		else {
			TFT_Send_Command(g_Show_Print_page[1], 12, 0);
		}
	}

	while (lower_index > 0) {
#ifdef Keyboard_Emu
		key = getchar();
#else
#ifdef USE_KEY_QUEUE
		key = Get_key_queue(1);
#else
		key = User_key;
		User_key = Null_key;
#endif
#endif
		if (key==Up_key || key==Down_key || key==Enter_key || key==Power_key) {
		}
		else
			if (key==Refresh_key) {
				goto show_and_print;
			}
			else
				continue;


		if (key==Up_key) {
			if (NewRegion_index > lower_index) {
				NewRegion_index--;
				//highlight change
				change_highlight = 1;
				goto show_and_print_menu;
				//Region_index = NewRegion_index;
			}
			else {
				if (lower_index1 > 0) {
					lower_index1--;
					if (Load_sheet(&temp_mea1, &sheet_info[circular_index[lower_index1]-1], circular_index[lower_index1]-1)==-1) {
						printf("up_key: load flash error\n");
						return;
					}
				}
				else {
					show_back = 1;
					//TFT_Send_Command(g_Show_Print_page[28], 12, 0);
				}
				if (upper_index1 >= (MAX_Page_Sheets-1)) {
					upper_index1--;
					goto show_and_print_menu;
				}
			}
		}
		else
			if (key==Down_key) {
				if (NewRegion_index < upper_index) {
					NewRegion_index++;
					//highlight hand-off
					//i = Region_index - show_back + lower_index1 -3
					//i = NewRegion_index - show_back + lower_index1 -3
					change_highlight = 1;
					goto show_and_print_menu;
				}
				else {
					if (upper_index==9) {
						if (upper_index1 < (total_sheets-1)) {						//show up scroll arrow
							upper_index1++;
							if (Load_sheet(&temp_mea1, &sheet_info[circular_index[upper_index1]-1], circular_index[upper_index1]-1)==-1) {
								printf("down_key: load flash error\n");
								return;
							}
							//TFT_Send_Command(g_Show_Print_page[27], 12, 0);
							if (show_back==0)
								lower_index1++;
							else
								show_back = 0;
							goto show_and_print_menu;
						}
						else {
							// keep current state
						}
					}
					else {
						// keep current state
					}
				}
			}
			if (key==Enter_key) {
				if (Region_index==3 && show_back==1)
					return;
				TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
				Select_sheet = circular_index[Region_index - 3 + lower_index1-show_back];

#ifdef USE_DATA_FLASH
				if (Select_sheet <= 0 && Select_sheet > MAX_Sheets)
					printf("select sheet index error: %d", Select_sheet);
#endif
				Show_and_Print();
				goto show_and_print;
			}
			else
				if (key==Power_key) {
					if (!is_Power_Down()) {
						goto show_and_print;
					}
					else {
						outpw((uint32_t)&GPIOE->DOUT, inpw((uint32_t) & GPIOE->PIN) & ~(1 << 5));
						while(1);
					}
				}
	}
}


void Show_and_Print()
{
	int i, j, selected_index[MAX_Samples+1], upper_index = 5, lower_index = 1, Region_index = 1, active_length, lower_index1 = -1, upper_index1 = -1, key, redraw_screen = 1, key1, comfirm_retry = 0, NewRegion_index = 1;
	int change_highlight = 0;
	char buf[10];
	struct Measure_tran temp_mea1;

#ifdef EMI_test
	upper_index = 5;
#else
	upper_index = 4;
#endif

	if (Load_sheet(&temp_mea1, &sheet_info[Select_sheet-1], Select_sheet-1)==-1) {
		printf("load flash error\n");
		return;
	}
	for (i = 0; i < sizeof(selected_index)/sizeof(int); i++)
		selected_index[i] = -1;

	if ((temp_mea1.sample_index+temp_mea1.blank_flag) > 0) {
		lower_index1 = 0;
		if (temp_mea1.sample_index+temp_mea1.blank_flag > 3)
			upper_index1 = 3;
		else
			upper_index1 = temp_mea1.sample_index+temp_mea1.blank_flag;
	}

Print_Page_redraw:
	if (!change_highlight) {
		TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
		//    Delay100ms(3);
		TFT_Send_Command(g_Show_Print_page1[0], 12, 0);
	}

	for (i = 1; i < 5; i++)
		if ((i==Region_index && !change_highlight) || (i==NewRegion_index && change_highlight)) {
			switch (i) {
			   case 1:
				   TFT_Send_Command(g_Show_Print_page1[2], 12, 0);
				   TFT_Send_Command(g_Show_Print_page1[25], 12, 0);
				   TFT_Send_Command(g_Show_Print_page1[30], 12, 0);
				   break;
			   case 2:
				   TFT_Send_Command(g_Show_Print_page1[4], 12, 0);
				   TFT_Send_Command(g_Show_Print_page1[26], 12, 0);
				   TFT_Send_Command(g_Show_Print_page1[30], 12, 0);
				   break;
			   case 3:
				   TFT_Send_Command(g_Show_Print_page1[6], 12, 0);
				   TFT_Send_Command(g_Show_Print_page1[27], 12, 0);
				   TFT_Send_Command(g_Show_Print_page1[31], 12, 0);
				   break;
			   case 4:
				   TFT_Send_Command(g_Show_Print_page1[8], 12, 0);
				   TFT_Send_Command(g_Show_Print_page1[28], 12, 0);
				   TFT_Send_Command(g_Show_Print_page1[32], 12, 0);
				   break;
			}
		}
		else {
			switch (i) {
              case 1:
				  TFT_Send_Command(g_Show_Print_page1[1], 12, 0);
             //TFT_Send_Command(g_Show_Print_page1[25], 12, 0);
             //TFT_Send_Command(g_Show_Print_page1[30], 12, 0);
				  break;
			  case 2:
				  TFT_Send_Command(g_Show_Print_page1[3], 12, 0);
				  //TFT_Send_Command(g_Show_Print_page1[26], 12, 0);
				  //TFT_Send_Command(g_Show_Print_page1[30], 12, 0);
				  break;
			  case 3:
				  TFT_Send_Command(g_Show_Print_page1[5], 12, 0);
				  //TFT_Send_Command(g_Show_Print_page1[27], 12, 0);
				  //TFT_Send_Command(g_Show_Print_page1[31], 12, 0);
				  break;
			  case 4:
				  TFT_Send_Command(g_Show_Print_page1[7], 12, 0);
				  //TFT_Send_Command(g_Show_Print_page1[28], 12, 0);
				  //TFT_Send_Command(g_Show_Print_page1[32], 12, 0);
				  break;
			}
		}

		if (change_highlight) {
			change_highlight = 0;
			Region_index = NewRegion_index;
		}
		if (Region_index <= 4)
			TFT_Send_Command(g_Show_Print_page1[10], 12, 0);

		sprintf(buf, "%02d", Select_sheet);
		Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Show_Print_page1[9][5], g_Show_Print_page1[9][7]);
		Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Show_Print_page1[9][5]-8, g_Show_Print_page1[9][7]);

Print_Sheet_redraw:
		if (redraw_screen==1) {
			for (i = 6, j = lower_index1; i < 11; i++, j++) {
				//Show_LCD_IMG(g_Show_Print_page1, 24, g_Show_Print_page1[24][5], g_Show_Print_page1[24][7]-(i-6)*24);
				if (i < (6+2+upper_index1) ) {
					if (lower_index1 > 0) {
						sprintf(buf, "%02d", temp_mea1.sample_No[j-1]);
						if ((NewRegion_index==i && change_highlight) || (Region_index==i && !change_highlight)) {
							//             printf("sheet redraw type1, lower_index1: %d, upper_index1: %d, i: %d, NewRegion_index: %d, Region_index: %d %s\n", lower_index1, upper_index1, i, NewRegion_index, Region_index, buf);
							//             Delay100ms(3);
							Show_LCD_IMG(g_Show_Print_page1, 21, g_Show_Print_page1[21][5], g_Show_Print_page1[21][7]-(i-6-2)*24);  //sample_xx
#ifndef LOCCUS_ODM
							Show_LCD_IMG(gDigital_image+10, buf[0]-'0', g_Show_Print_page1[22][5], g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
							Show_LCD_IMG(gDigital_image+10, buf[1]-'0', g_Show_Print_page1[23][5], g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#else
							Show_LCD_IMG(gDigital_image+10, buf[0]-'0', g_Show_Print_page1[22][5]-8, g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
							Show_LCD_IMG(gDigital_image+10, buf[1]-'0', g_Show_Print_page1[23][5]-8, g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#endif
							TFT_Send_Command(g_Show_Print_page1[11], sizeof(g_Show_Print_page1[11]),0);
							Display_measure_output1(&temp_mea1, sheet_info[Select_sheet-1].sheet_type, (upper_index1 - (upper_index - i))-1);  //kkk
						}
						else 
							if ((Region_index==i && change_highlight) || !change_highlight){
								//                printf("sheet redraw type1, lower_index1: %d, upper_index1: %d, i: %d, NewRegion_index: %d, Region_index: %d %s\n", lower_index1, upper_index1, i, NewRegion_index, Region_index, buf);
								//                Delay100ms(3);
								Show_LCD_IMG(g_Show_Print_page1, 20, g_Show_Print_page1[20][5], g_Show_Print_page1[20][7]-(i-6-2)*24);  //sample_xx
#ifndef LOCCUS_ODM
								Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Show_Print_page1[22][5], g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
								Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Show_Print_page1[23][5], g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#else
								Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Show_Print_page1[22][5]-8, g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
								Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Show_Print_page1[23][5]-8, g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#endif
							}
							if (selected_index[j]==1) {
								Show_LCD_IMG(g_Show_Print_page1, 33, g_Show_Print_page1[33][5], g_Show_Print_page1[33][7]-(i-6-2)*24);  //selection checked symbol
							}
					}
					else
						if (upper_index1<=3) {
							if (i==6) {
								//                printf("sheet redraw type2, lower_index1: %d, upper_index1: %d, i: %d, NewRegion_index: %d, Region_index: %d\n", lower_index1, upper_index1, i, NewRegion_index, Region_index);
								//                Delay100ms(3);
								if ((NewRegion_index==i && change_highlight) || (Region_index==i && !change_highlight)) {
									TFT_Send_Command(g_Show_Print_page1[17], sizeof(g_Show_Print_page1[17]),0);  //Return highlight
									TFT_Send_Command(g_Show_Print_page1[10], sizeof(g_Show_Print_page1[10]),0);
									TFT_Send_Command(g_Show_Print_page1[29], sizeof(g_Show_Print_page1[29]),0);
								}
								else
									if ((Region_index==i && change_highlight) || !change_highlight)
										TFT_Send_Command(g_Show_Print_page1[16], sizeof(g_Show_Print_page1[16]),0);  //Return
							}
							else
								if (i==7) {
									//                   printf("sheet redraw type2, lower_index1: %d, upper_index1: %d, i: %d, NewRegion_index: %d, Region_index: %d\n", lower_index1, upper_index1, i, NewRegion_index, Region_index);
									//                   Delay100ms(3);
									if ((NewRegion_index==i && change_highlight) || (Region_index==i && !change_highlight)) {
										TFT_Send_Command(g_Show_Print_page1[19], sizeof(g_Show_Print_page1[19]),0);  //blank highlight
										TFT_Send_Command(g_Show_Print_page1[11], sizeof(g_Show_Print_page1[11]),0);
										Display_measure_output1(&temp_mea1, 0, 0);  //kkk
									}
									else
										if ((Region_index==i && change_highlight) || !change_highlight)
											TFT_Send_Command(g_Show_Print_page1[18], sizeof(g_Show_Print_page1[18]),0);  //blank
								}
								else {
									//                    printf("sheet redraw type2, lower_index1: %d, upper_index1: %d, i: %d, NewRegion_index: %d, Region_index: %d\n", lower_index1, upper_index1, i, NewRegion_index, Region_index);
									//                    Delay100ms(3);
									sprintf(buf, "%02d", temp_mea1.sample_No[i-8]);
									//                    printf("sample%s%d\n", buf, temp_mea1.sample_No[i-8]);
									if ((NewRegion_index==i && change_highlight) || (Region_index==i && !change_highlight)) {
										Show_LCD_IMG(g_Show_Print_page1, 21, g_Show_Print_page1[21][5], g_Show_Print_page1[21][7]-(i-8)*24);  //sample_xx highlight
#ifndef LOCCUS_ODM
										Show_LCD_IMG(gDigital_image+10, buf[0]-'0', g_Show_Print_page1[22][5], g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
										Show_LCD_IMG(gDigital_image+10, buf[1]-'0', g_Show_Print_page1[23][5], g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#else
										Show_LCD_IMG(gDigital_image+10, buf[0]-'0', g_Show_Print_page1[22][5]-8, g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
										Show_LCD_IMG(gDigital_image+10, buf[1]-'0', g_Show_Print_page1[23][5]-8, g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#endif
										TFT_Send_Command(g_Show_Print_page1[11], sizeof(g_Show_Print_page1[11]),0);
										Display_measure_output1(&temp_mea1, sheet_info[Select_sheet-1].sheet_type, (upper_index1 - (upper_index - i))-1);  //kkk
									}
									else 
										if ((Region_index==i && change_highlight) || !change_highlight) {
											Show_LCD_IMG(g_Show_Print_page1, 20, g_Show_Print_page1[20][5], g_Show_Print_page1[20][7]-(i-8)*24);  //sample_xx
#ifndef LOCCUS_ODM
											Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Show_Print_page1[22][5], g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
											Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Show_Print_page1[23][5], g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#else
											Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Show_Print_page1[22][5]-8, g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
											Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Show_Print_page1[23][5]-8, g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#endif
										}
								}
								if (i >= 7 && selected_index[i-7]==1) {
									Show_LCD_IMG(g_Show_Print_page1, 33, g_Show_Print_page1[33][5], g_Show_Print_page1[33][7]-(i-6-2)*24);  //selection checked symbol
								}
						}
						else {
							if (i==6) {
								//                 printf("sheet redraw type3, lower_index1: %d, upper_index1: %d, i: %d, NewRegion_index: %d, Region_index: %d\n", lower_index1, upper_index1, i, NewRegion_index, Region_index);
								//                 Delay100ms(3);
								if ((NewRegion_index==i && change_highlight) || (Region_index==i && !change_highlight)) {
									Show_LCD_IMG(g_Show_Print_page1, 19, g_Show_Print_page1[19][5], g_Show_Print_page1[19][7]+24);  //blank highlight
									//TFT_Send_Command(g_Display_blank_output[23], sizeof(g_Display_blank_output[23]),0);
									TFT_Send_Command(g_Show_Print_page1[11], sizeof(g_Show_Print_page1[11]),0);
									Display_measure_output1(&temp_mea1, 0, 0);
								}
								else
									Show_LCD_IMG(g_Show_Print_page1, 18, g_Show_Print_page1[18][5], g_Show_Print_page1[18][7]+24);  //blank
							}
							else {
								//                  printf("sheet redraw type3, lower_index1: %d, upper_index1: %d, i: %d, NewRegion_index: %d, Region_index: %d\n", lower_index1, upper_index1, i, NewRegion_index, Region_index);
								//                  Delay100ms(3);
								sprintf(buf, "%02d", temp_mea1.sample_No[i-7]);
								if ((NewRegion_index==i && change_highlight) || (Region_index==i && !change_highlight)) {
									Show_LCD_IMG(g_Show_Print_page1, 21, g_Show_Print_page1[21][5], g_Show_Print_page1[21][7]-(i-8)*24);  //sample_xx highlight
#ifndef LOCCUS_ODM
									Show_LCD_IMG(gDigital_image+10, buf[0]-'0', g_Show_Print_page1[22][5], g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
									Show_LCD_IMG(gDigital_image+10, buf[1]-'0', g_Show_Print_page1[23][5], g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#else
									Show_LCD_IMG(gDigital_image+10, buf[0]-'0', g_Show_Print_page1[22][5]-8, g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
									Show_LCD_IMG(gDigital_image+10, buf[1]-'0', g_Show_Print_page1[23][5]-8, g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#endif
									TFT_Send_Command(g_Show_Print_page1[11], sizeof(g_Show_Print_page1[11]),0);
									printf("Select_sheet: %d\n", Select_sheet);
									Display_measure_output1(&temp_mea1, sheet_info[Select_sheet-1].sheet_type, (upper_index1 - (upper_index - i))-1);
								}
								else 
									if ((Region_index==i && change_highlight) || !change_highlight) {
										Show_LCD_IMG(g_Show_Print_page1, 20, g_Show_Print_page1[20][5], g_Show_Print_page1[20][7]-(i-8)*24);  //sample_xx
#ifndef LOCCUS_ODM
										Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Show_Print_page1[22][5], g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
										Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Show_Print_page1[23][5], g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#else
										Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Show_Print_page1[22][5]-8, g_Show_Print_page1[22][7]-(i-6-2)*24);  //sample list first digit
										Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Show_Print_page1[23][5]-8, g_Show_Print_page1[23][7]-(i-6-2)*24);  //sample list second digit
#endif
									}
							}

							if (i >= 6 && selected_index[i-6]==1) {
								Show_LCD_IMG(g_Show_Print_page1, 33, g_Show_Print_page1[33][5], g_Show_Print_page1[33][7]-(i-6-2)*24);  //selection checked symbol
							}
						}
				}
			}  //for

			if (upper_index1==(temp_mea1.sample_index+temp_mea1.blank_flag)) {		//redraw down arrow
				TFT_Send_Command(g_Show_Print_page1[13], 12, 0);
			}
			else
				if (upper_index1 < (temp_mea1.sample_index+temp_mea1.blank_flag)) {
					TFT_Send_Command(g_Show_Print_page1[12], 12, 0);
				}

				if (upper_index1 > 3) {		//redraw up arrow
					TFT_Send_Command(g_Show_Print_page1[14], 12, 0);
				}
				else
					if (upper_index1 <= 3) {
						TFT_Send_Command(g_Show_Print_page1[15], 12, 0);
					}

					if (change_highlight) {
						change_highlight = 0;
						Region_index = NewRegion_index;
					}
					redraw_screen = 0;
		}

		while(1) {
#ifdef Keyboard_Emu
			key = getchar();
#else
#ifdef USE_KEY_QUEUE
			key = Get_key_queue(1);
#else
			key = User_key;
			User_key = Null_key;
#endif
#endif
			if (key==Up_key) {
				if (NewRegion_index > lower_index) {
					NewRegion_index--;
					change_highlight = 1;
					if (lower_index==1)
						goto Print_Page_redraw;
					else {
						redraw_screen = 1;
						goto Print_Sheet_redraw;
					}
				}
				else {
					if (lower_index==6) {
						if (upper_index1 > 3) {
							if (lower_index1 > 0)
								lower_index1--;
							upper_index1--;
							redraw_screen = 1;
							goto Print_Sheet_redraw;
						}
						else {
							upper_index1 = temp_mea1.sample_index+temp_mea1.blank_flag;
							if (upper_index1==3) {
								lower_index1 = 0;
								NewRegion_index = Region_index = 10;
								redraw_screen = 1;
								goto Print_Sheet_redraw;
							}
							else
								if (upper_index1 >= 4) {
									lower_index1 = upper_index1 - 4;
									NewRegion_index = Region_index = 10;
									redraw_screen = 1;
									goto Print_Sheet_redraw;
								}
						}
					}
					else {
						// keep current state
					}
				}
			}
			else
				if (key==Down_key) {
					if (NewRegion_index < upper_index) {
						NewRegion_index++;
						change_highlight = 1;
						if (upper_index==4 || upper_index==5)
							goto Print_Page_redraw;
						else {
							redraw_screen = 1;
							goto Print_Sheet_redraw;
						}
					}
					else {
						if (upper_index==10) {
							if (upper_index1 < (temp_mea1.sample_index+temp_mea1.blank_flag)) {
								if (upper_index1 > 3)
									lower_index1++;
								upper_index1++;
								redraw_screen = 1;
								goto Print_Sheet_redraw;
							}
							else {
								lower_index1 = 0;
								upper_index1 = 3;
								NewRegion_index = Region_index = 6;
								redraw_screen = 1;
								goto Print_Sheet_redraw;
							}
						}
						else {
							// keep current state
						}
					}   
				}
				else
					if (key==Enter_key) {
						switch(Region_index) {
			  case 1:
				  //		         printf("temp_mea1.sample_index+temp_mea1.blank_flag=%d\n", temp_mea1.sample_index+temp_mea1.blank_flag);
				  for (i = 0, j = 0; i < sizeof(selected_index)/sizeof(int); i++)
					  if (i <= (temp_mea1.sample_index+temp_mea1.blank_flag))
						  if (selected_index[i]==1)
							  j++;

				  if ((temp_mea1.sample_index+temp_mea1.blank_flag) >= j) {  //Has select all
					  for (i = 0, j = 0; i < sizeof(selected_index)/sizeof(int); i++)
						  if (i <= (temp_mea1.sample_index+temp_mea1.blank_flag))
							  selected_index[i] = 1;
				  }
				  else {
					  for (i = 0, j = 0; i < sizeof(selected_index)/sizeof(int); i++)
						  if (i <= (temp_mea1.sample_index+temp_mea1.blank_flag))
							  selected_index[i] = -1;
				  }

				  redraw_screen = 1;
				  goto Print_Sheet_redraw;
				  break;
			  case 2:
				  if (temp_mea1.blank_flag > 0) {
					  lower_index = 6;
					  upper_index = lower_index+temp_mea1.sample_index+temp_mea1.blank_flag+1;
					  NewRegion_index = Region_index = lower_index;
					  if (upper_index > 10)
						  upper_index = 10;

					  TFT_Send_Command(g_Show_Print_page1[3], 12, 0);
					  TFT_Send_Command(g_Show_Print_page1[17], 12, 0);
					  TFT_Send_Command(g_Show_Print_page1[29], 12, 0);
				  }
				  break;

			  case 3:
			      bPrinter_error = 0;
				  while (Print_Selected(&temp_mea1, selected_index, &sheet_info[Select_sheet-1])) {
					  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
					  TFT_Send_Command(g_Msg_page[53], 12, 0);  //Printer failed msg
					  TFT_Send_Command(g_Msg_page[54], 12,0);   //=======
					  for (i = 0, j = 0; j < 6; j++)
						  if (bPrinter_error & (1 << j)) {
							  if (i > 0) {
#ifndef LOCCUS_ODM
								  Show_LCD_IMG(g_Msg_page, 64, g_Msg_page[63][5]-i*8, g_Msg_page[64][7]);
#else
								  Show_LCD_IMG(g_Msg_page, 64, g_Msg_page[63][5]-i*8-40, g_Msg_page[64][7]);
#endif
								  i++;
							  }
							  sprintf(buf, "%02d, ", j+1);
#ifndef LOCCUS_ODM
							  Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Msg_page[63][5]-i*8, g_Msg_page[63][7]);
#else
							  Show_LCD_IMG(gDigital_image, buf[0]-'0', g_Msg_page[63][5]-i*8-40, g_Msg_page[63][7]);
#endif
							  i++;
#ifndef LOCCUS_ODM
							  Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Msg_page[63][5]-i*8, g_Msg_page[63][7]);
#else
							  Show_LCD_IMG(gDigital_image, buf[1]-'0', g_Msg_page[63][5]-i*8-40, g_Msg_page[63][7]);
#endif
							  i++;
						  }

						  TFT_Send_Command(g_Msg_page[56], 12,0);  //Retry button with highlight
						  TFT_Send_Command(g_Msg_page[57], 12,0);  //Cancel button without highlight
						  TFT_Send_Command(g_Msg_page[59], 12,0);  //Enter key for Retry button
						  TFT_Send_Command(g_Msg_page[62], 12,0);  //Enter key mask for Cancel button

						  comfirm_retry = 1;
#ifdef Keyboard_Emu
						  while ((key1 = getchar()) != Enter_key)
#else
						  while (1)
#endif
						  {
#ifdef Keyboard_Emu
#else
#ifdef USE_KEY_QUEUE
							  key1 = Get_key_queue(0);
#else
							  key1 = User_key;
							  User_key = Null_key;
#endif
							  if (key1==Enter_key)
								  break;
#endif
							  if (comfirm_retry==1) {
								  if (key1==Down_key) {
									  TFT_Send_Command(g_Msg_page[55], 12,0);  //Retry button without highlight
									  TFT_Send_Command(g_Msg_page[58], 12,0);  //Cancel button with highlight
									  TFT_Send_Command(g_Msg_page[60], 12,0);  //Enter key mask for Retry button
									  TFT_Send_Command(g_Msg_page[61], 12,0);  //Enter key for Cancel button
									  comfirm_retry = 0;
								  }
							  }
							  else 
								  if (key1==Up_key) {
									  TFT_Send_Command(g_Msg_page[56], 12,0);  //Retry button with highlight
									  TFT_Send_Command(g_Msg_page[57], 12,0);  //Cancel button without highlight
									  TFT_Send_Command(g_Msg_page[59], 12,0);  //Enter key for Retry button
									  TFT_Send_Command(g_Msg_page[62], 12,0);  //Enter key mask for Cancel button
									  comfirm_retry = 1;
								  }
						  }

						  if (comfirm_retry==0) {
							  redraw_screen = 1;
							  goto Print_Page_redraw;
							  break;
						  }
				  }

				  TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
				  TFT_Send_Command(g_Msg_page[65], 12,0);  //printing complete!
				  Delay100ms(10);
				  redraw_screen = 1;
				  goto Print_Page_redraw;
				  break;

			  case 4:
				  return;
				  break;

			  case 6:
			  case 7:
			  case 8:
			  case 9:
			  case 10:
				  measure_index = (upper_index1 - (upper_index - Region_index));
				  if (measure_index >= 0) {
					  if (selected_index[measure_index]==-1) {
						  /* buf2[1] = 0x00; buf2[0] = 0xfb; */
						  selected_index[measure_index] = 1;
						  Show_LCD_IMG(g_Show_Print_page1, 33, g_Show_Print_page1[33][5], g_Show_Print_page1[33][7]-(Region_index-6-2)*24);  //selection checked symbol
					  }
					  else {
						  /*buf2[1] = 0x00; buf2[0] = 0x20;*/
						  selected_index[measure_index] = -1;
						  Show_LCD_IMG(g_Show_Print_page1, 34, g_Show_Print_page1[34][5], g_Show_Print_page1[34][7]-(Region_index-6-2)*24);  //selection checked symbol
					  }
					  //Show_text1(28, (Region_index-5)*16, buf2);
				  }
				  else
					  if (measure_index < 0 && Region_index==6) {
						  lower_index = 1;
#ifdef EMI_test
						  upper_index = 5;
#else
						  upper_index = 4;
#endif
						  NewRegion_index = Region_index = 3;
						  TFT_Send_Command(g_Show_Print_page1[6], 12, 0);
						  TFT_Send_Command(g_Show_Print_page1[16], 12, 0);
						  TFT_Send_Command(g_Show_Print_page1[27], 12, 0);
						  TFT_Send_Command(g_Show_Print_page1[31], 12, 0);
					  }  
					  break;
						}
					}


					if (key==Refresh_key) {
						redraw_screen = 1;
						goto Print_Page_redraw; 
					}
					else
						continue;
		}
}

/*20130515 added by michael*/
void Time_Setup()
{
	int i, key, Region_index = 1, temp, RTC_field = 0, redraw_time = 1;
	char days_of_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	union MaestroDate temp_date, temp_date1;
	char buf[10];

	temp_date.PackDate.Second = 25;
	temp_date.PackDate.Minute = 27;
	temp_date.PackDate.Hour = 7;
	temp_date.PackDate.Day = 11;
	temp_date.PackDate.Month = 9;
	temp_date.PackDate.Year = 2001;

Redraw_Time_Screen:
	TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
	TFT_Send_Command(g_Time_Setting_page[0], 12, 0);  //time setting

	switch (Region_index) {
	  case 1:
		  TFT_Send_Command(g_Time_Setting_page[1], 12, 0);  //up arrow
		  TFT_Send_Command(g_Time_Setting_page[11], 12, 0);  //down arrow
		  break;
	  case 2:
		  TFT_Send_Command(g_Time_Setting_page[2], 12, 0);  //up arrow
		  TFT_Send_Command(g_Time_Setting_page[12], 12, 0);  //down arrow
		  break;
	  case 3:
		  TFT_Send_Command(g_Time_Setting_page[3], 12, 0);  //up arrow
		  TFT_Send_Command(g_Time_Setting_page[13], 12, 0);  //down arrow
		  break;
	  case 4:
		  TFT_Send_Command(g_Time_Setting_page[4], 12, 0);  //up arrow
		  TFT_Send_Command(g_Time_Setting_page[14], 12, 0);  //down arrow
		  break;
	  case 5:
		  TFT_Send_Command(g_Time_Setting_page[5], 12, 0);  //up arrow
		  TFT_Send_Command(g_Time_Setting_page[15], 12, 0);  //down arrow
		  break;
	}

	DrvTIMER_Init();
	DrvTIMER_Open(TMR0,2,PERIODIC_MODE);
	DrvTIMER_SetTimerEvent(TMR0,2, (TIMER_CALLBACK)TMR_Callback,0);
	DrvTIMER_EnableInt(TMR0);
#ifdef USE_BSP_ver_June_2011
	DrvTIMER_Start(TMR0);
#endif  //USE_BSP_ver_June_2011

	refresh_time = 1;
	while(1) {
		if (refresh_time) {
			temp = 0;
			if (is_RTC_Low_Power()) {
				TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
				TFT_Send_Command(g_Msg_page[66], 12, 0);  //low battery msg
				Delay100ms(35);
				return;
			}
			else
				Get_Maestro_RTC(&temp_date, i);

			if ((temp_date.PackDate.Hour != temp_date1.PackDate.Hour) || redraw_time) {
				sprintf(buf, "%02d", temp_date.PackDate.Hour);
				Show_LCD_Number(buf, (g_Time_Setting_page[21][5] | g_Time_Setting_page[21][4] << 8), g_Time_Setting_page[21][7]);
			}
			printf("Hour %s\n", buf);

			if ((temp_date.PackDate.Minute != temp_date1.PackDate.Minute) || redraw_time) {
				sprintf(buf, "%02d", temp_date.PackDate.Minute);
				Show_LCD_Number(buf, (g_Time_Setting_page[22][5] | g_Time_Setting_page[22][4] << 8), g_Time_Setting_page[22][7]);
			}
			printf("Minute %s\n", buf);


			if ((temp_date.PackDate.Second != temp_date1.PackDate.Second) || redraw_time) {
				sprintf(buf, "%02d", temp_date.PackDate.Second);
				Show_LCD_Number(buf, (g_Time_Setting_page[23][5] | g_Time_Setting_page[23][4] << 8), g_Time_Setting_page[23][7]);
			}
			printf("Second %s\n", buf);

			if ((temp_date.PackDate.Year != temp_date1.PackDate.Year) || redraw_time) {
				sprintf(buf, "%4d", temp_date.PackDate.Year);
				Show_LCD_Number(buf, (g_Time_Setting_page[24][5] | g_Time_Setting_page[24][4] << 8), g_Time_Setting_page[24][7]);
			}
			printf("Year %s\n", buf);

			if ((temp_date.PackDate.Month != temp_date1.PackDate.Month) || redraw_time) {
				sprintf(buf, "%02d", temp_date.PackDate.Month);
				Show_LCD_Number(buf, (g_Time_Setting_page[25][5] | g_Time_Setting_page[25][4] << 8), g_Time_Setting_page[25][7]);
			}
			printf("Month %s\n", buf);

			if ((temp_date.PackDate.Day != temp_date1.PackDate.Day) || redraw_time) {
				sprintf(buf, "%02d", temp_date.PackDate.Day);
				Show_LCD_Number(buf, (g_Time_Setting_page[26][5] | g_Time_Setting_page[26][4] << 8), g_Time_Setting_page[26][7]);
			}
			printf("Day %s\n", buf);
			temp_date1 = temp_date;
			redraw_time = 0;
			refresh_time = 0;
		}  //refresh_time==1

#ifdef Keyboard_Emu
		key = getchar();
#else
#ifdef USE_KEY_QUEUE
		key = Get_key_queue(1);
#else
		key = User_key;
		User_key = Null_key;
#endif
#endif

		if (key==Enter_key) {
			Region_index++;

			switch(Region_index) {
	          case 2:
				  TFT_Send_Command(g_Time_Setting_page[6], 12, 0);  //up arrow mask
				  TFT_Send_Command(g_Time_Setting_page[16], 12, 0);  //down arrow mask
				  TFT_Send_Command(g_Time_Setting_page[2], 12, 0);  //up arrow
				  TFT_Send_Command(g_Time_Setting_page[12], 12, 0);  //down arrow
				  break;
			  case 3:
				  TFT_Send_Command(g_Time_Setting_page[7], 12, 0);  //up arrow mask
				  TFT_Send_Command(g_Time_Setting_page[17], 12, 0);  //down arrow mask
				  TFT_Send_Command(g_Time_Setting_page[3], 12, 0);  //up arrow
				  TFT_Send_Command(g_Time_Setting_page[13], 12, 0);  //down arrow
				  break;
			  case 4:
				  TFT_Send_Command(g_Time_Setting_page[8], 12, 0);  //up arrow mask
				  TFT_Send_Command(g_Time_Setting_page[18], 12, 0);  //down arrow mask
				  TFT_Send_Command(g_Time_Setting_page[4], 12, 0);  //up arrow
				  TFT_Send_Command(g_Time_Setting_page[14], 12, 0);  //down arrow
				  break;
			  case 5:
				  TFT_Send_Command(g_Time_Setting_page[9], 12, 0);  //up arrow mask
				  TFT_Send_Command(g_Time_Setting_page[19], 12, 0);  //down arrow mask
				  TFT_Send_Command(g_Time_Setting_page[5], 12, 0);  //up arrow
				  TFT_Send_Command(g_Time_Setting_page[15], 12, 0);  //down arrow         
				  break;
			  case 6:
				  return;
			}
		}  //key==Enter_key
		else
			if (key==Up_key) {
				switch (Region_index) {
                  case 1:
					  temp_date.PackDate.Hour++;
					  if (temp_date.PackDate.Hour==24)
						  temp_date.PackDate.Hour = 0;
					  sprintf(buf, "%02d", temp_date.PackDate.Hour);
					  Show_LCD_Number(buf, (g_Time_Setting_page[21][5] | g_Time_Setting_page[21][4] << 8), g_Time_Setting_page[21][7]);
					  RTC_field = 2;
					  break;
				  case 2:  
					  temp_date.PackDate.Minute++;
					  if (temp_date.PackDate.Minute==60)
						  temp_date.PackDate.Minute = 0;
					  sprintf(buf, "%02d", temp_date.PackDate.Minute);
					  Show_LCD_Number(buf, (g_Time_Setting_page[22][5] | g_Time_Setting_page[22][4] << 8), g_Time_Setting_page[22][7]);
					  RTC_field = 1;
					  break;
				  case 3:
					  temp_date.PackDate.Year++;
					  sprintf(buf, "%02d", temp_date.PackDate.Year);
					  Show_LCD_Number(buf, (g_Time_Setting_page[24][5] | g_Time_Setting_page[24][4] << 8), g_Time_Setting_page[24][7]);
					  if ((temp_date.PackDate.Year%4==0 && temp_date.PackDate.Year%100!=0)||temp_date.PackDate.Year%400==0)
						  days_of_month[1] = 29;
					  else
						  days_of_month[1] = 28;
					  RTC_field = 5;
					  break;
				  case 4:  
					  temp_date.PackDate.Month++;
					  if (temp_date.PackDate.Month==13)
						  temp_date.PackDate.Month = 1;
					  sprintf(buf, "%02d", temp_date.PackDate.Month);
					  Show_LCD_Number(buf, (g_Time_Setting_page[25][5] | g_Time_Setting_page[25][4] << 8), g_Time_Setting_page[25][7]);
					  RTC_field = 4;
					  break;
				  case 5:  
					  if (temp_date.PackDate.Day < days_of_month[temp_date.PackDate.Month-1])
						  temp_date.PackDate.Day++;
					  else
						  temp_date.PackDate.Day = 1;
					  sprintf(buf, "%02d", temp_date.PackDate.Day);
					  Show_LCD_Number(buf, (g_Time_Setting_page[26][5] | g_Time_Setting_page[26][4] << 8), g_Time_Setting_page[26][7]);
					  RTC_field = 3;
					  break;
				}
			}  //key==Up_key
			else
				if (key==Down_key) {
					switch (Region_index) {
                      case 1:  
						  temp_date.PackDate.Hour--;
						  if (temp_date.PackDate.Hour==0x1f)
							  temp_date.PackDate.Hour = 23;
						  sprintf(buf, "%02d", temp_date.PackDate.Hour);
						  Show_LCD_Number(buf, (g_Time_Setting_page[21][5] | g_Time_Setting_page[21][4] << 8), g_Time_Setting_page[21][7]);
						  RTC_field = 2;
						  break;
					  case 2:  
						  temp_date.PackDate.Minute--;
						  if (temp_date.PackDate.Minute==0x3f)
							  temp_date.PackDate.Minute = 59;
						  sprintf(buf, "%02d", temp_date.PackDate.Minute);
						  Show_LCD_Number(buf, (g_Time_Setting_page[22][5] | g_Time_Setting_page[22][4] << 8), g_Time_Setting_page[22][7]);
						  RTC_field = 1;
						  break;
					  case 3:  
						  temp_date.PackDate.Year--;
						  sprintf(buf, "%02d", temp_date.PackDate.Year);
						  Show_LCD_Number(buf, (g_Time_Setting_page[24][5] | g_Time_Setting_page[24][4] << 8), g_Time_Setting_page[24][7]);
						  RTC_field = 5;
						  break;
					  case 4:  
						  temp_date.PackDate.Month--;
						  if (temp_date.PackDate.Month==0)
							  temp_date.PackDate.Month = 12;
						  sprintf(buf, "%02d", temp_date.PackDate.Month);
						  Show_LCD_Number(buf, (g_Time_Setting_page[25][5] | g_Time_Setting_page[25][4] << 8), g_Time_Setting_page[25][7]);
						  RTC_field = 4;
						  break;
					  case 5:  
						  temp_date.PackDate.Day--;
						  if (temp_date.PackDate.Day==0)
							  temp_date.PackDate.Day = days_of_month[temp_date.PackDate.Month-1];
						  sprintf(buf, "%02d", temp_date.PackDate.Day);
						  Show_LCD_Number(buf, (g_Time_Setting_page[26][5] | g_Time_Setting_page[26][4] << 8), g_Time_Setting_page[26][7]);
						  RTC_field = 3;
						  break;
					}
				}  //key==Down_key
				else
					if (key==Power_key) {
						if (!is_Power_Down()) {
							refresh_time = 1;
							redraw_time = 1;
							goto Redraw_Time_Screen;
						}
						else {
							outpw((uint32_t)&GPIOE->DOUT, inpw((uint32_t) & GPIOE->PIN) & ~(1 << 5));
							while(1);
						}
					}  //key==Power_key
					else 
						if (key==Refresh_key) {
							refresh_time = 1;
							redraw_time = 1;
							goto Redraw_Time_Screen;			 
						}  //key==Refresh_key

		if (key==Up_key || key==Down_key) {
			if (RTC_field > 0) {
				if (is_RTC_Low_Power()) {
					TFT_Send_Command(gMain_menu_page[0], 12, 0);  //background
					TFT_Send_Command(g_Msg_page[66], 12, 0);  //low battery msg
					Delay100ms(35);
					return;
				}
				else
					Set_Maestro_RTC(&temp_date, RTC_field);
				DrvI2C_InstallCallback(I2C_PORT0, I2CFUNC, I2C0_Callback_Tx);
				DrvI2C_InstallCallback(I2C_PORT0, TIMEOUT, I2C0_Timeout);
				DrvI2C_EnableTimeoutCount(I2C_PORT0, 1, 1);
				/* I2C0 as master sends START signal */
				start_I2C = 0;
				EndFlag0 = 0;
				DrvI2C_Ctrl(I2C_PORT0, 1, 0, 0, 0);
				while (EndFlag0 == 0);
				refresh_time = 0;
				DrvI2C_UninstallCallBack(I2C_PORT0, I2CFUNC);
				DrvI2C_UninstallCallBack(I2C_PORT0, TIMEOUT);
				DrvI2C_EnableTimeoutCount(I2C_PORT0, 0, 0);
			}
		}
	}  //while(1)

	DrvTIMER_ClearTimerEvent(TMR0, 0);
	DrvTIMER_Close(TMR0);
}